Improves emulator: Adds getopt with debug and visual mode. Adds cmpi mnemonic and changes semantic of cmp.
This commit is contained in:
parent
a24fb63449
commit
f73d831119
3 changed files with 85 additions and 34 deletions
|
@ -3,6 +3,8 @@ CC = gcc
|
|||
|
||||
all: explink expdump emulator as
|
||||
|
||||
.PHONY: as clean run
|
||||
|
||||
explink: explink.c
|
||||
$(CC) -g -o bin/$@ $^
|
||||
|
||||
|
@ -19,4 +21,14 @@ test: exp
|
|||
./exp -o test.exp $(ARGS)
|
||||
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
|
102
libvm/emulator.c
102
libvm/emulator.c
|
@ -1,9 +1,11 @@
|
|||
#define _CRT_SECURE_NO_WARNINGS
|
||||
|
||||
#include <stdbool.h>
|
||||
#include "vm.h"
|
||||
#include "exp.h"
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <getopt.h>
|
||||
|
||||
#if defined(_MSC_VER)
|
||||
#include <SDL.h>
|
||||
#undef main
|
||||
|
@ -12,6 +14,8 @@
|
|||
#endif
|
||||
|
||||
bool running = true;
|
||||
bool debugMode = false;
|
||||
bool visualMode = false;
|
||||
|
||||
/**
|
||||
* An assertion the VM does.
|
||||
|
@ -54,14 +58,13 @@ spu_t mainCore;
|
|||
void dump_vm()
|
||||
{
|
||||
printf(
|
||||
"%8X %3d %3d %1X [",
|
||||
"cp=%8X bp=%3d f=%1X [",
|
||||
mainCore.codePointer,
|
||||
mainCore.stackPointer,
|
||||
mainCore.basePointer,
|
||||
mainCore.flags
|
||||
);
|
||||
|
||||
for (int i = 1; i < mainCore.stackPointer; i++)
|
||||
for (int i = 0; i < mainCore.stackPointer; i++)
|
||||
{
|
||||
printf(" %d", mainCore.stack[i]);
|
||||
}
|
||||
|
@ -73,7 +76,7 @@ void update_vm()
|
|||
{
|
||||
vm_step_process(&mainCore);
|
||||
|
||||
// dump_vm();
|
||||
if(debugMode) dump_vm();
|
||||
}
|
||||
|
||||
void update_input(SDL_Event *ev)
|
||||
|
@ -83,6 +86,10 @@ void update_input(SDL_Event *ev)
|
|||
case SDL_QUIT:
|
||||
running = false;
|
||||
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;
|
||||
}
|
||||
|
||||
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) {
|
||||
return 1;
|
||||
exit(1);
|
||||
}
|
||||
atexit(SDL_Quit);
|
||||
|
||||
SDL_Surface *screen = SDL_SetVideoMode(640, 480, 32, SDL_DOUBLEBUF);
|
||||
SDL_WM_SetCaption("DasOS Virtual Platform", NULL);
|
||||
|
||||
dump_vm();
|
||||
|
||||
|
||||
SDL_Event ev;
|
||||
while (running)
|
||||
{
|
||||
|
@ -226,17 +220,61 @@ int main(int argc, char **argv)
|
|||
|
||||
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;
|
||||
}
|
||||
|
||||
/*#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*/
|
|
@ -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 } },
|
||||
{ "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 } },
|
||||
{ "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 } },
|
||||
{ "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 } },
|
||||
|
|
Loading…
Reference in a new issue