Commit da585c7803339cc84b0bb6f381e179b40cfdaf89

Authored by Imanol-Mikel Barba Sabariego
1 parent db7e2683

Debugger fully functional and non-quirky :)

.idea/synacorvm.iml
... ... @@ -6,12 +6,14 @@
6 6 <sourceFolder url="file://$MODULE_DIR$/stack.c" isTestSource="false" />
7 7 <sourceFolder url="file://$MODULE_DIR$/cpu.c" isTestSource="false" />
8 8 <sourceFolder url="file://$MODULE_DIR$/disasm.c" isTestSource="false" />
  9 + <sourceFolder url="file://$MODULE_DIR$/instructions.h" isTestSource="false" />
9 10 <sourceFolder url="file://$MODULE_DIR$/mem.h" isTestSource="false" />
10 11 <sourceFolder url="file://$MODULE_DIR$/main.c" isTestSource="false" />
11 12 <sourceFolder url="file://$MODULE_DIR$/mem.c" isTestSource="false" />
12 13 <sourceFolder url="file://$MODULE_DIR$/registers.h" isTestSource="false" />
13   - <sourceFolder url="file://$MODULE_DIR$/instructions.h" isTestSource="false" />
  14 + <sourceFolder url="file://$MODULE_DIR$/input.h" isTestSource="false" />
14 15 <sourceFolder url="file://$MODULE_DIR$/CMakeLists.txt" isTestSource="false" />
  16 + <sourceFolder url="file://$MODULE_DIR$/input.c" isTestSource="false" />
15 17 <sourceFolder url="file://$MODULE_DIR$/registers.c" isTestSource="false" />
16 18 <sourceFolder url="file://$MODULE_DIR$/debug.h" isTestSource="false" />
17 19 <sourceFolder url="file://$MODULE_DIR$/stack.h" isTestSource="false" />
... ...
CMakeLists.txt
... ... @@ -3,5 +3,5 @@ project(synacorvm)
3 3  
4 4 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
5 5  
6   -set(SOURCE_FILES main.c cpu.c cpu.h registers.h mem.h disasm.h disasm.c stack.c stack.h registers.c mem.c instructions.h debug.c debug.h)
  6 +set(SOURCE_FILES main.c cpu.c cpu.h registers.h mem.h disasm.h disasm.c stack.c stack.h registers.c mem.c instructions.h debug.c debug.h input.c input.h)
7 7 add_executable(synacorvm ${SOURCE_FILES})
... ...
... ... @@ -224,7 +224,11 @@ void out(uint16_t src)
224 224  
225 225 void in(uint16_t dst)
226 226 {
227   - int c = getchar();
  227 + if(!input_buffer_size())
  228 + {
  229 + read_input();
  230 + }
  231 + char c = get_input();
228 232 regs[dst % (MAX_INT + 1)] = (uint16_t)c;
229 233 }
230 234  
... ...
... ... @@ -10,6 +10,7 @@
10 10 #include "stack.h"
11 11 #include "registers.h"
12 12 #include "instructions.h"
  13 +#include "input.h"
13 14  
14 15 #define MAX_INT 32767
15 16  
... ...
... ... @@ -71,7 +71,6 @@ void save_state(char *filename)
71 71  
72 72 void query()
73 73 {
74   - //TODO discard stdin
75 74 uint8_t end = 0;
76 75 uint16_t bpoint;
77 76 uint16_t mempos;
... ... @@ -97,8 +96,8 @@ void query()
97 96 switch (command)
98 97 {
99 98 case 'b':
100   - sscanf(input,"b %hu\n",&bpoint);
101   - fprintf(stderr,"Set breakpoint %hu to %hu",nbpoints,bpoint);
  99 + sscanf(input,"b %hx\n",&bpoint);
  100 + fprintf(stderr,"Set breakpoint %hu to %hx",nbpoints,bpoint);
102 101 breakpoints[nbpoints] = bpoint;
103 102 nbpoints = (uint16_t)(nbpoints + 1 % 0xFF);
104 103 break;
... ... @@ -301,12 +300,16 @@ void debug_program()
301 300 if(in_breakpoint(pc))
302 301 {
303 302 breakpoint = 1;
304   - fprintf(stderr,"Breakpoint hit @ pc=%02X",pc);
  303 + fprintf(stderr,"Breakpoint hit @ pc=%X",pc);
305 304 }
306 305 if(breakpoint)
307 306 {
308 307 fprintf(stderr, "%02X: ", pc);
309 308 print_instruction(mem[pc], mem[pc+1], mem[pc+2], mem[pc+3]);
  309 + if(stdin_length())
  310 + {
  311 + read_input();
  312 + }
310 313 query();
311 314 if(terminate)
312 315 {
... ...
input.c 0 → 100644
  1 +//
  2 +// Created by imanol on 12/27/16.
  3 +//
  4 +
  5 +#include "input.h"
  6 +
  7 +unsigned int input_buffer_length = 0;
  8 +unsigned int input_buffer_pos = 0;
  9 +char input_buffer[MAX_INT];
  10 +
  11 +unsigned int stdin_length()
  12 +{
  13 + unsigned int length = 0;
  14 + ioctl(0, I_NREAD, &length);
  15 + return length;
  16 +}
  17 +
  18 +void read_input()
  19 +{
  20 + strncpy(input_buffer,input_buffer+input_buffer_pos,input_buffer_size());
  21 + input_buffer_pos = input_buffer_size();
  22 + unsigned int old_buffer_length = input_buffer_length;
  23 + input_buffer_length = stdin_length();
  24 + if(!input_buffer_length)
  25 + {
  26 + input_buffer_length = 64; //arbitrary
  27 + }
  28 + fgets(input_buffer+input_buffer_pos,input_buffer_length,stdin);
  29 + //TODO: Improve this... Get a way to know how many bytes read
  30 + input_buffer_length = (unsigned int)strlen(input_buffer);
  31 + //input_buffer_length += old_buffer_length;
  32 +}
  33 +
  34 +char get_input()
  35 +{
  36 + char c = input_buffer[input_buffer_pos++];
  37 + return c;
  38 +}
  39 +
  40 +unsigned int input_buffer_size()
  41 +{
  42 + return input_buffer_length - (input_buffer_pos);
  43 +}
0 44 \ No newline at end of file
... ...
input.h 0 → 100644
  1 +//
  2 +// Created by imanol on 12/27/16.
  3 +//
  4 +
  5 +#ifndef SYNACORVM_INPUT_H
  6 +#define SYNACORVM_INPUT_H
  7 +
  8 +#include <stdint.h>
  9 +#include <unistd.h>
  10 +#include "cpu.h"
  11 +#include <string.h>
  12 +#include <stropts.h>
  13 +
  14 +unsigned int stdin_length();
  15 +void read_input();
  16 +char get_input();
  17 +unsigned int input_buffer_size();
  18 +
  19 +#endif //SYNACORVM_INPUT_H
... ...