Improves emulator: Adds getopt with debug and visual mode. Adds cmpi mnemonic and changes semantic of cmp.

This commit is contained in:
Felix Queißner 2016-07-01 10:28:25 +02:00
parent a24fb63449
commit f73d831119
3 changed files with 85 additions and 34 deletions

View file

@ -3,6 +3,8 @@ CC = gcc
all: explink expdump emulator as all: explink expdump emulator as
.PHONY: as clean run
explink: explink.c explink: explink.c
$(CC) -g -o bin/$@ $^ $(CC) -g -o bin/$@ $^
@ -19,4 +21,14 @@ test: exp
./exp -o test.exp $(ARGS) ./exp -o test.exp $(ARGS)
hexdump -C test.exp hexdump -C test.exp
.PHONY: as run: test.exp
./bin/emulator -d $^
%.exp: %.bin
./bin/explink -o $@ -c $^
%.bin: %.asm
./bin/as -o $@ -L $^
clean:
rm *.bin

View file

@ -1,9 +1,11 @@
#define _CRT_SECURE_NO_WARNINGS #define _CRT_SECURE_NO_WARNINGS
#include <stdbool.h>
#include "vm.h" #include "vm.h"
#include "exp.h" #include "exp.h"
#include <stdbool.h>
#include <getopt.h>
#if defined(_MSC_VER) #if defined(_MSC_VER)
#include <SDL.h> #include <SDL.h>
#undef main #undef main
@ -12,6 +14,8 @@
#endif #endif
bool running = true; bool running = true;
bool debugMode = false;
bool visualMode = false;
/** /**
* An assertion the VM does. * An assertion the VM does.
@ -54,14 +58,13 @@ spu_t mainCore;
void dump_vm() void dump_vm()
{ {
printf( printf(
"%8X %3d %3d %1X [", "cp=%8X bp=%3d f=%1X [",
mainCore.codePointer, mainCore.codePointer,
mainCore.stackPointer,
mainCore.basePointer, mainCore.basePointer,
mainCore.flags mainCore.flags
); );
for (int i = 1; i < mainCore.stackPointer; i++) for (int i = 0; i < mainCore.stackPointer; i++)
{ {
printf(" %d", mainCore.stack[i]); printf(" %d", mainCore.stack[i]);
} }
@ -73,7 +76,7 @@ void update_vm()
{ {
vm_step_process(&mainCore); vm_step_process(&mainCore);
// dump_vm(); if(debugMode) dump_vm();
} }
void update_input(SDL_Event *ev) void update_input(SDL_Event *ev)
@ -83,6 +86,10 @@ void update_input(SDL_Event *ev)
case SDL_QUIT: case SDL_QUIT:
running = false; running = false;
break; break;
case SDL_KEYDOWN:
if(ev->key.keysym.sym == SDLK_ESCAPE)
running = false;
break;
} }
} }
@ -157,29 +164,16 @@ bool load_exp(const char *fileName)
return true; return true;
} }
int main(int argc, char **argv) void run_visual_mode()
{ {
// Required before ANY virtual machine memory operations...
initialize_vm();
// Load the EXP file
if (argc > 1)
{
if (!load_exp(argv[1])) {
return 1;
}
}
if (SDL_Init(SDL_INIT_VIDEO) != 0) { if (SDL_Init(SDL_INIT_VIDEO) != 0) {
return 1; exit(1);
} }
atexit(SDL_Quit); atexit(SDL_Quit);
SDL_Surface *screen = SDL_SetVideoMode(640, 480, 32, SDL_DOUBLEBUF); SDL_Surface *screen = SDL_SetVideoMode(640, 480, 32, SDL_DOUBLEBUF);
SDL_WM_SetCaption("DasOS Virtual Platform", NULL); SDL_WM_SetCaption("DasOS Virtual Platform", NULL);
dump_vm();
SDL_Event ev; SDL_Event ev;
while (running) while (running)
{ {
@ -226,17 +220,61 @@ int main(int argc, char **argv)
SDL_Delay(32); SDL_Delay(32);
} }
}
void run_text_mode()
{
while (running)
{
//TODO: Insert some kind of text events.
update_vm();
}
}
int main(int argc, char **argv)
{
// Required before ANY virtual machine memory operations...
initialize_vm();
opterr = 0;
int c;
while ((c = getopt(argc, argv, "dV")) != -1)
{
switch (c)
{
case 'd':
debugMode = 1;
break;
case 'V':
visualMode = 1;
break;
case '?':
if (optopt == 'o' || optopt == 'c' || optopt == 'd')
fprintf(stderr, "Option -%c requires an argument.\n", optopt);
else if (isprint(optopt))
fprintf(stderr, "Unknown option `-%c'.\n", optopt);
else
fprintf(stderr, "Unknown option character `\\x%x'.\n", optopt);
return 1;
default:
abort();
}
}
for (int index = optind; index < argc; index++)
{
fprintf(stdout, "Loading %s...\n", argv[index]);
if(load_exp(argv[index]) == 0) {
fprintf(stderr, "%s not found.\n", argv[index]);
exit(1);
}
}
if(debugMode) dump_vm();
if(visualMode)
run_visual_mode();
else
run_text_mode();
return 0; return 0;
} }
/*#if defined(_MSC_VER)*/
/*FILE * __cdecl __iob_func(void)*/
/*{*/
/*static FILE iob[3];*/
/*iob[0] = *stdin;*/
/*iob[1] = *stdout;*/
/*iob[2] = *stderr;*/
/*return iob;*/
/*}*/
/*#endif*/

View file

@ -24,7 +24,8 @@ const mnemonic_t mnemonics[] =
{ "cpget",{ VM_EXEC_X, VM_EXEC_X, VM_INPUT_ZERO, VM_INPUT_ZERO, VM_CMD_CPGET, 1, VM_FLAG_NO, VM_OUTPUT_PUSH } }, { "cpget",{ VM_EXEC_X, VM_EXEC_X, VM_INPUT_ZERO, VM_INPUT_ZERO, VM_CMD_CPGET, 1, VM_FLAG_NO, VM_OUTPUT_PUSH } },
{ "add",{ VM_EXEC_X, VM_EXEC_X, VM_INPUT_POP, VM_INPUT_POP, VM_CMD_MATH, 0, VM_FLAG_NO, VM_OUTPUT_PUSH } }, { "add",{ VM_EXEC_X, VM_EXEC_X, VM_INPUT_POP, VM_INPUT_POP, VM_CMD_MATH, 0, VM_FLAG_NO, VM_OUTPUT_PUSH } },
{ "sub",{ VM_EXEC_X, VM_EXEC_X, VM_INPUT_POP, VM_INPUT_POP, VM_CMD_MATH, 1, VM_FLAG_NO, VM_OUTPUT_PUSH } }, { "sub",{ VM_EXEC_X, VM_EXEC_X, VM_INPUT_POP, VM_INPUT_POP, VM_CMD_MATH, 1, VM_FLAG_NO, VM_OUTPUT_PUSH } },
{ "cmp",{ VM_EXEC_X, VM_EXEC_X, VM_INPUT_POP, VM_INPUT_POP, VM_CMD_MATH, 1, VM_FLAG_YES, VM_OUTPUT_DISCARD } }, { "cmp",{ VM_EXEC_X, VM_EXEC_X, VM_INPUT_ARG, VM_INPUT_POP, VM_CMD_MATH, 1, VM_FLAG_YES, VM_OUTPUT_DISCARD } },
{ "cmpi",{ VM_EXEC_X, VM_EXEC_X, VM_INPUT_POP, VM_INPUT_POP, VM_CMD_MATH, 1, VM_FLAG_YES, VM_OUTPUT_DISCARD } },
{ "mul",{ VM_EXEC_X, VM_EXEC_X, VM_INPUT_POP, VM_INPUT_POP, VM_CMD_MATH, 2, VM_FLAG_NO, VM_OUTPUT_PUSH } }, { "mul",{ VM_EXEC_X, VM_EXEC_X, VM_INPUT_POP, VM_INPUT_POP, VM_CMD_MATH, 2, VM_FLAG_NO, VM_OUTPUT_PUSH } },
{ "div",{ VM_EXEC_X, VM_EXEC_X, VM_INPUT_POP, VM_INPUT_POP, VM_CMD_MATH, 3, VM_FLAG_NO, VM_OUTPUT_PUSH } }, { "div",{ VM_EXEC_X, VM_EXEC_X, VM_INPUT_POP, VM_INPUT_POP, VM_CMD_MATH, 3, VM_FLAG_NO, VM_OUTPUT_PUSH } },
{ "mod",{ VM_EXEC_X, VM_EXEC_X, VM_INPUT_POP, VM_INPUT_POP, VM_CMD_MATH, 4, VM_FLAG_NO, VM_OUTPUT_PUSH } }, { "mod",{ VM_EXEC_X, VM_EXEC_X, VM_INPUT_POP, VM_INPUT_POP, VM_CMD_MATH, 4, VM_FLAG_NO, VM_OUTPUT_PUSH } },