added video enable option
This commit is contained in:
parent
7876210be6
commit
5397965319
1
.vscode/c_cpp_properties.json
vendored
1
.vscode/c_cpp_properties.json
vendored
@ -5,6 +5,7 @@
|
||||
"defines": [],
|
||||
"includePath": [
|
||||
"dependencies/tlibc/include",
|
||||
"src",
|
||||
"${default}"
|
||||
],
|
||||
"cStandard": "c11"
|
||||
|
||||
5
.vscode/launch.json
vendored
5
.vscode/launch.json
vendored
@ -7,7 +7,10 @@
|
||||
"request": "launch",
|
||||
"program": "${workspaceFolder}/bin/tcpu",
|
||||
"windows": { "program": "${workspaceFolder}/bin/tcpu.exe" },
|
||||
"args": [ "-c", "../examples/loop.tasm", "o.bin", "--debug", "-i", "o.bin" ],
|
||||
"args": [
|
||||
"-c", "../examples/video.tasm", "o.bin",
|
||||
"-i", "o.bin", "--debug", "--video"
|
||||
],
|
||||
"cwd": "${workspaceFolder}/bin",
|
||||
"preLaunchTask": "build_exec_dbg",
|
||||
"stopAtEntry": false,
|
||||
|
||||
2
dependencies/tlibc
vendored
2
dependencies/tlibc
vendored
@ -1 +1 @@
|
||||
Subproject commit 8eeaff4245d0876f5c3308991ede7696254487f7
|
||||
Subproject commit c415e2ca8ff51f41984ace8fe796187e6ad0fa27
|
||||
@ -3,5 +3,5 @@ Example of graphical application
|
||||
*/
|
||||
|
||||
.main:
|
||||
//TODO: write code here
|
||||
//TODO: add a way to access Event struct's fields
|
||||
exit
|
||||
@ -26,7 +26,7 @@ OBJDIR="obj"
|
||||
OUTDIR="bin"
|
||||
STATIC_LIB_FILE="lib$PROJECT.a"
|
||||
|
||||
INCLUDE="-I./dependencies/tlibc/include"
|
||||
INCLUDE="-Isrc -Idependencies/tlibc/include"
|
||||
|
||||
# OS-specific options
|
||||
case "$OS" in
|
||||
|
||||
@ -1,69 +1,70 @@
|
||||
#include <SDL3/SDL.h>
|
||||
#include <SDL3_image/SDL_image.h>
|
||||
#include "Display.h"
|
||||
#include "tcpu_version.h"
|
||||
|
||||
typedef struct Display {
|
||||
str name;
|
||||
i32 width;
|
||||
i32 height;
|
||||
SDL_Window* window;
|
||||
SDL_Renderer* renderer;
|
||||
} Display;
|
||||
|
||||
static SDL_InitState sdl_init_state = {0};
|
||||
static SDL_InitState _sdl_init_state = {0};
|
||||
static Display _d = {0};
|
||||
static cstr _title = "TCPU v" TCPU_VERSION_CSTR;
|
||||
|
||||
bool Display_init(i32 w, i32 h, DisplayFlags flags){
|
||||
_d.width = w;
|
||||
_d.height = h;
|
||||
_d.window = NULL;
|
||||
_d.renderer = NULL;
|
||||
|
||||
bool Display_tryConstruct(Display* d, str name, i32 w, i32 h, DisplayFlags flags){
|
||||
d->name = str_copy(name);
|
||||
d->width = w;
|
||||
d->height = h;
|
||||
d->window = NULL;
|
||||
d->renderer = NULL;
|
||||
|
||||
if (SDL_ShouldInit(&sdl_init_state)) {
|
||||
if (SDL_ShouldInit(&_sdl_init_state)) {
|
||||
bool sdl_initialized = SDL_Init(SDL_INIT_VIDEO);
|
||||
SDL_SetInitialized(&sdl_init_state, sdl_initialized);
|
||||
SDL_SetInitialized(&_sdl_init_state, sdl_initialized);
|
||||
if(!sdl_initialized)
|
||||
return NULL;
|
||||
return false;
|
||||
}
|
||||
|
||||
SDL_WindowFlags window_flags = SDL_WINDOW_ALWAYS_ON_TOP;
|
||||
if(!SDL_CreateWindowAndRenderer(d->name.data, d->width, d->height, window_flags, &d->window, &d->renderer)){
|
||||
return NULL;
|
||||
if(!SDL_CreateWindowAndRenderer(_title, _d.width, _d.height, window_flags, &_d.window, &_d.renderer)){
|
||||
return false;
|
||||
}
|
||||
|
||||
return d;
|
||||
return true;
|
||||
}
|
||||
|
||||
void Display_destroy(Display* d){
|
||||
free(d->name.data);
|
||||
SDL_DestroyRenderer(d->renderer);
|
||||
SDL_DestroyWindow(d->window);
|
||||
free(d);
|
||||
void Display_destroy(){
|
||||
SDL_DestroyRenderer(_d.renderer);
|
||||
SDL_DestroyWindow(_d.window);
|
||||
|
||||
// if (SDL_ShouldQuit(&sdl_init_state)) {
|
||||
// if (SDL_ShouldQuit(&_sdl_init_state)) {
|
||||
// SDL_Quit();
|
||||
// SDL_SetInitialized(&sdl_init_state, false);
|
||||
// SDL_SetInitialized(&_sdl_init_state, false);
|
||||
// }
|
||||
}
|
||||
|
||||
bool Display_setName(Display* d, str name){
|
||||
d->name = str_copy(name);
|
||||
return SDL_SetWindowTitle(d->window, name.data);
|
||||
NULLABLE(cstr) Display_getError(){
|
||||
return SDL_GetError();
|
||||
}
|
||||
|
||||
bool Display_setSize(Display* d, u32 w, u32 h){
|
||||
d->width = w;
|
||||
d->height = h;
|
||||
return SDL_SetWindowSize(d->window, w, h);
|
||||
bool Display_setSize(u32 w, u32 h){
|
||||
_d.width = w;
|
||||
_d.height = h;
|
||||
return SDL_SetWindowSize(_d.window, w, h);
|
||||
}
|
||||
|
||||
bool Display_setDrawingColor(Display* d, ColorRGBA color){
|
||||
return SDL_SetRenderDrawColor(d->renderer, color.r, color.g, color.b, color.a);
|
||||
bool Display_setFullScreenMode(bool value){
|
||||
return SDL_SetWindowFullscreen(_d.window, value);
|
||||
}
|
||||
|
||||
bool Display_clear(Display* d){
|
||||
return SDL_RenderClear(d->renderer);
|
||||
bool Display_setDrawingColor(ColorRGBA color){
|
||||
return SDL_SetRenderDrawColor(_d.renderer, color.r, color.g, color.b, color.a);
|
||||
}
|
||||
|
||||
bool Display_clear(){
|
||||
return SDL_RenderClear(_d.renderer);
|
||||
}
|
||||
|
||||
#define Rect_copy(DST, SRC) {\
|
||||
@ -73,16 +74,12 @@ bool Display_clear(Display* d){
|
||||
DST.h = SRC.h;\
|
||||
}
|
||||
|
||||
bool Display_fillRect(Display* d, Rect rect) {
|
||||
bool Display_fillRect(Rect rect) {
|
||||
SDL_FRect sdl_rect;
|
||||
Rect_copy(sdl_rect, rect);
|
||||
return SDL_RenderFillRect(d->renderer, &sdl_rect);
|
||||
return SDL_RenderFillRect(_d.renderer, &sdl_rect);
|
||||
}
|
||||
|
||||
bool Display_swapBuffers(Display* d){
|
||||
return SDL_RenderPresent(d->renderer);
|
||||
}
|
||||
|
||||
NULLABLE(cstr) Display_getError(){
|
||||
return SDL_GetError();
|
||||
bool Display_swapBuffers(){
|
||||
return SDL_RenderPresent(_d.renderer);
|
||||
}
|
||||
|
||||
@ -18,15 +18,13 @@ typedef enum DisplayFlags {
|
||||
DisplayFlags_Default = 0
|
||||
} DisplayFlags;
|
||||
|
||||
typedef struct Display Display;
|
||||
|
||||
bool Display_tryConstruct(Display* d, str name, i32 w, i32 h, DisplayFlags flags);
|
||||
void Display_destroy(Display* d);
|
||||
|
||||
bool Display_setName(Display* d, str name);
|
||||
bool Display_setSize(Display* d, u32 w, u32 h);
|
||||
bool Display_setDrawingColor(Display* d, ColorRGBA color);
|
||||
bool Display_clear(Display* d);
|
||||
bool Display_fillRect(Display* d, Rect rect);
|
||||
bool Display_swapBuffers(Display* d);
|
||||
bool Display_init(i32 w, i32 h, DisplayFlags flags);
|
||||
void Display_destroy();
|
||||
NULLABLE(cstr) Display_getError();
|
||||
|
||||
bool Display_setSize(u32 w, u32 h);
|
||||
bool Display_setFullScreenMode(bool value);
|
||||
bool Display_setDrawingColor(ColorRGBA color);
|
||||
bool Display_clear();
|
||||
bool Display_fillRect(Rect rect);
|
||||
bool Display_swapBuffers();
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
#include "VM.h"
|
||||
#include "../instructions/instructions.h"
|
||||
#include "instructions/instructions.h"
|
||||
|
||||
void VM_construct(VM* vm){
|
||||
memset(vm, 0, sizeof(VM));
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
#pragma once
|
||||
#include "tlibc/std.h"
|
||||
#include "tlibc/string/str.h"
|
||||
#include "../instructions/registers.h"
|
||||
#include "instructions/registers.h"
|
||||
|
||||
typedef union Register {
|
||||
u64 rx;
|
||||
|
||||
@ -1,8 +1,8 @@
|
||||
#pragma once
|
||||
#include "tlibc/std.h"
|
||||
#include "tlibc/string/str.h"
|
||||
#include "../instructions/instructions.h"
|
||||
#include "../instructions/registers.h"
|
||||
#include "instructions/instructions.h"
|
||||
#include "instructions/registers.h"
|
||||
#include "tlibc/collections/List.h"
|
||||
|
||||
typedef enum ArgumentType {
|
||||
|
||||
@ -1,8 +1,8 @@
|
||||
#pragma once
|
||||
#include "tlibc/std.h"
|
||||
#include "tlibc/string/str.h"
|
||||
#include "../instructions/instructions.h"
|
||||
#include "../instructions/registers.h"
|
||||
#include "instructions/instructions.h"
|
||||
#include "instructions/registers.h"
|
||||
#include "tlibc/collections/List.h"
|
||||
#include "tlibc/collections/HashMap.h"
|
||||
#include "AST.h"
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
#pragma once
|
||||
#include "../instructions.h"
|
||||
#include "../registers.h"
|
||||
#include "instructions/instructions.h"
|
||||
#include "instructions/registers.h"
|
||||
|
||||
#define readVar(VAR) {\
|
||||
if(!VM_dataRead(vm, &VAR, vm->current_pos, sizeof(VAR))) \
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
#pragma once
|
||||
#include "../VM/VM.h"
|
||||
#include "VM/VM.h"
|
||||
|
||||
///@param program_pos position in vm->program next afrer opcode
|
||||
///@returns number of bytes read
|
||||
|
||||
36
src/main.c
36
src/main.c
@ -1,8 +1,9 @@
|
||||
#include "VM/VM.h"
|
||||
#include "instructions/instructions.h"
|
||||
#include "tlibc/collections/List.h"
|
||||
#include "compiler/Compiler.h"
|
||||
#include "VM/Display/Display.h"
|
||||
#include "tcpu_version.h"
|
||||
#include "tlibc/time.h"
|
||||
|
||||
#define arg_is(LITERAL) str_equals(arg_str, STR(LITERAL))
|
||||
|
||||
@ -23,16 +24,18 @@ i32 main(const i32 argc, cstr* argv){
|
||||
cstr NULLABLE(source_file) = NULL;
|
||||
|
||||
bool debug_log = false;
|
||||
bool video_enabled = false;
|
||||
|
||||
for(i32 argi = 1; argi < argc; argi++){
|
||||
str arg_str = str_from_cstr(argv[argi]);
|
||||
if(arg_is("-h") || arg_is("--help")){
|
||||
printf(
|
||||
"-h, --help Show this message.\n"
|
||||
"-op, --opcodes Show list of all instructions.\n"
|
||||
"-i, --image [FILE] Boot VM using image file.\n"
|
||||
"-c, --compile [SOURCE_FILE] [OUT_FILE] Compile assembly source files to machine code.\n"
|
||||
"-d, --debug Enable debug log.\n"
|
||||
"-op, --opcodes Show list of all instructions.\n"
|
||||
"-c, --compile [SOURCE_FILE] [OUT_FILE] Compile assembly source files to machine code.\n"
|
||||
"-i, --image [FILE] Boot VM using image file.\n"
|
||||
"--video Enable VM display.\n"
|
||||
);
|
||||
return 0;
|
||||
}
|
||||
@ -80,6 +83,10 @@ i32 main(const i32 argc, cstr* argv){
|
||||
else if(arg_is("-d") || arg_is("--debug")){
|
||||
debug_log = true;
|
||||
}
|
||||
else if(arg_is("--video")){
|
||||
video_enabled = true;
|
||||
}
|
||||
|
||||
else {
|
||||
printfe("ERROR: unknown argument '%s'\n", argv[argi]);
|
||||
return 1;
|
||||
@ -89,12 +96,24 @@ i32 main(const i32 argc, cstr* argv){
|
||||
i32 exit_code = 0;
|
||||
if(compile){
|
||||
exit_code = compileSources(source_file, out_file, debug_log);
|
||||
if(exit_code != 0)
|
||||
goto main_exit;
|
||||
}
|
||||
if(exit_code == 0 && boot){
|
||||
|
||||
if(boot){
|
||||
printfe("TCPU version: " TCPU_VERSION_CSTR "\n");
|
||||
if(video_enabled){
|
||||
printfe("video enabled\n");
|
||||
if(!Display_init(1600, 900, DisplayFlags_Default)){
|
||||
printfe("DISPLAY ERROR: %s\n", Display_getError());
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
exit_code = bootFromImage(image_file);
|
||||
}
|
||||
|
||||
// frees global variables to supress valgrind memory leak errors
|
||||
main_exit:
|
||||
Instruction_destroySearchStructs();
|
||||
return exit_code;
|
||||
}
|
||||
@ -123,7 +142,10 @@ i32 bootFromImage(cstr image_file){
|
||||
|
||||
i32 exit_code = 1;
|
||||
if(VM_setMemory(&vm, vm_memory, bytes_read)){
|
||||
printf("===============================================================================\n");
|
||||
exit_code = VM_boot(&vm);
|
||||
printf("===============================================================================\n");
|
||||
printfe("VM stopped with code %i\n", exit_code);
|
||||
}
|
||||
if(vm.state == VMState_InternalError){
|
||||
if(vm.error_message){
|
||||
@ -133,10 +155,6 @@ i32 bootFromImage(cstr image_file){
|
||||
else printfe("VM ERROR: unknown (error_message is null)\n");
|
||||
}
|
||||
|
||||
if(exit_code != 0){
|
||||
printfe("program exited with code %i\n", exit_code);
|
||||
}
|
||||
|
||||
free(vm_memory);
|
||||
return exit_code;
|
||||
}
|
||||
|
||||
3
src/tcpu_version.h
Normal file
3
src/tcpu_version.h
Normal file
@ -0,0 +1,3 @@
|
||||
#pragma once
|
||||
|
||||
#define TCPU_VERSION_CSTR "1.0.0"
|
||||
Loading…
Reference in New Issue
Block a user