#include <stdio.h>
+#include <stdio_ext.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <unistd.h>
-#include "tigr.h"
+#include "/home/bylex/software/tigr/tigr.h"
#include "common.h"
#include "opcodes.h"
fclose(input_file);
}
+void quit()
+{
+ __fpurge(stdin);
+ printf("\nExecution halted: cause %d\n", halted_reason);
+ tigrUpdate(screen);
+ printf("Press any key to exit...\n");
+ getc(stdin);
+ exit(0);
+}
+void run_fast()
+{
+ while(!halted)
+ {
+ uint8_t instruction = (uint8_t)mem[current_instruction];
+ uint8_t arg1 = (uint8_t)(mem[current_instruction] >> 8);
+ uint8_t arg2 = (uint8_t)mem[current_instruction + 1];
+ uint8_t arg3 = (uint8_t)(mem[current_instruction + 1] >> 8);
+ (*opcodes[instruction]) (arg1, arg2, arg3);
+ instruction_counter++;
+ if(force_draw)
+ {
+ tigrUpdate(screen);
+ force_draw = false;
+ }
+ }
+ quit();
+}
-int main(int argc, char *argv[])
+
+void run_debug()
{
- screen = tigrWindow(256,256, "Interpreter display", 0);
- if(argc == 1)
+ bool run = false;
+ while(!halted)
{
- fprintf(stderr, "No memory file!\n");
- exit(1);
+ uint8_t instruction = (uint8_t)mem[current_instruction];
+ uint8_t arg1 = (uint8_t)(mem[current_instruction] >> 8);
+ uint8_t arg2 = (uint8_t)mem[current_instruction + 1];
+ uint8_t arg3 = (uint8_t)(mem[current_instruction + 1] >> 8);
+ bool next = false;
+ while(!next)
+ {
+ if(breakpoints_size == 0 && run)
+ {
+ run_fast();
+ }
+ else
+ {
+ if(get_breakpoint(current_instruction))
+ {
+ printf("Break at 0x%04x\n", current_instruction);
+ run = false;
+ }
+ else if(run)
+ {
+ break;
+ }
+ }
+ printf("Executing at 0x%04x; Instruction number %ld\n", current_instruction, instruction_counter);
+ printf("[S]tep | [D]isassemble | [M]odify register | Set [B]reakpoint | [U]nset breakpoint | [R]un | [Q]uit\n");
+ int reg = -1;
+ int addr = -1;
+ char c;
+ c = getc(stdin);
+ while(c == '\n')
+ {
+ c = getc(stdin);
+ }
+ switch(c)
+ {
+ case 'd':
+ case 'D':
+ int i = max(current_instruction - 10, 0);
+ int upperbound = min(current_instruction + 10, 65534);
+ while(i < upperbound)
+ {
+ if(i == current_instruction)
+ {
+ printf("\x1b[1;31m");
+ print_instruction(i, (uint8_t)mem[i], (uint8_t)(mem[i] >> 8), (uint8_t)mem[i + 1], (uint8_t)(mem[i + 1] >> 8));
+ printf("\x1b[0;0m");
+ }
+ else if(i == current_instruction + 2)
+ {
+ printf("\x1b[1;32m");
+ print_instruction(i, (uint8_t)mem[i], (uint8_t)(mem[i] >> 8), (uint8_t)mem[i + 1], (uint8_t)(mem[i + 1] >> 8));
+ printf("\x1b[0;0m");
+
+ }
+ else
+ {
+ print_instruction(i, (uint8_t)mem[i], (uint8_t)(mem[i] >> 8), (uint8_t)mem[i + 1], (uint8_t)(mem[i + 1] >> 8));
+ }
+ i += 2;
+ }
+ break;
+ case 'm':
+ case 'M':
+ printf("Which register? ");
+ fflush(stdout);
+ scanf("%d", ®);
+ if (reg >= 0 && reg <= 255)
+ {
+ printf("Value to set(current value: %d)? ", regs[reg]);
+ fflush(stdout);
+ uint16_t val = 0;
+ scanf("%d", &val);
+ regs[reg] = val;
+ printf("OK regs[%d] = %d\n", reg, val);
+
+ }
+ else
+ {
+ printf("Invalid register\n");
+ }
+ break;
+ case 'q':
+ case 'Q':
+ halted_reason = 255;
+ quit();
+ break;
+ case 's':
+ case 'S':
+ next = true;
+ break;
+ case 'b':
+ case 'B':
+ printf("What address? ");
+ fflush(stdout);
+ scanf("%d", &addr);
+ set_breakpoint(addr);
+ break;
+ case 'u':
+ case 'U':
+ printf("What address? ");
+ fflush(stdout);
+ scanf("%d", &addr);
+ del_breakpoint(addr);
+ break;
+ case 'r':
+ case 'R':
+ run = true;
+ break;
+ }
+ }
+ (*opcodes[instruction]) (arg1, arg2, arg3);
+ instruction_counter++;
+ tigrUpdate(screen);
+
}
- load_mem(argv[1]);
+ quit();
+}
+
+void run_info()
+{
while(!halted)
{
uint8_t instruction = (uint8_t)mem[current_instruction];
fflush(stdout);
(*opcodes[instruction]) (arg1, arg2, arg3);
instruction_counter++;
-#ifndef DEBUG
- if(instruction_counter % 1024 == 0)
+ if(force_draw)
{
tigrUpdate(screen);
+ force_draw = false;
}
-#else
- tigrUpdate(screen);
-#endif
+ }
+ quit();
+}
+int main(int argc, char *argv[])
+{
+ screen = tigrWindow(256,256, "Interpreter display", 0);
+ if(argc == 1)
+ {
+ fprintf(stderr, "No memory file!\n");
+ exit(1);
}
- printf("\nExecution halted: cause %d\n", halted_reason);
- tigrUpdate(screen);
- printf("Press any key to exit...");
- getc(stdin);
-
+ load_mem(argv[1]);
+ if(argc == 2)
+ {
+ run_fast();
+ }
+ else if(strcmp(argv[2], "--debug") == 0)
+ {
+ run_debug();
+ }
+ else if(strcmp(argv[2], "--info") == 0)
+ {
+ run_info();
+ }
+ else
+ {
+ run_info();
+ }
}
extern uint8_t halted_reason;
extern Tigr* screen;
+extern bool force_draw;
void nop(uint8_t unused_1, uint8_t unused_2, uint8_t unused_3)
{
return;
}
+void fdr(uint8_t unused_1, uint8_t unused_2, uint8_t unused_3)
+{
+ force_draw = true;
+ current_instruction += 2;
+ return;
+}
+
+void (*opcodes[N_INSTRUCTIONS]) (uint8_t, uint8_t, uint8_t) = {nop, inc, dec, lod, ldl, sav, swp, jmp, jez, hlt, pts, pfs, dpx, lor, fdr};