Skip to content

Commit

Permalink
Fix data memory access violation bug and code optimize
Browse files Browse the repository at this point in the history
  • Loading branch information
yanorei32 committed Nov 29, 2019
1 parent bbb0b94 commit 784fb4f
Showing 1 changed file with 51 additions and 62 deletions.
113 changes: 51 additions & 62 deletions source/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,19 @@ typedef enum MODE {
} MODE;

unsigned char pram[CONSOLE_WIDTH*CONSOLE_HEIGHT];
unsigned char dram[128];

#define CHECK_DRAM_P_RANGE(dram_p) \
if (dram_p < 0) { \
iprintf("\nERROR: PTR < 0"); \
return; \
} \
if (sizeof(dram) / sizeof(char) <= dram_p) { \
iprintf("\nERROR: PTR >= RAM SIZE"); \
return; \
}

void run() {
unsigned char dram[128];
int dram_p, pram_p, block_cnt;
u16 key_state;
STATUS_FLAGS s;
Expand Down Expand Up @@ -75,55 +85,30 @@ void run() {
break;

case B_INC_VAL:
if (dram_p < 0) {
iprintf("\nERROR: PTR < 0");
return;
}

if (sizeof(dram) / sizeof(char) <= dram_p) {
iprintf("\nERROR: PTR >= RAM SIZE");
return;
}

CHECK_DRAM_P_RANGE(dram_p)
++dram[dram_p];

break;

case B_DEC_VAL:
if (dram_p < 0) {
iprintf("\nERROR: PTR < 0");
return;
}

if (sizeof(dram) / sizeof(char) <= dram_p) {
iprintf("\nERROR: PTR >= RAM SIZE");
return;
}

CHECK_DRAM_P_RANGE(dram_p)
--dram[dram_p];

break;

case B_PUT_VAL:
if (dram_p < 0) {
iprintf("\nERROR: PTR < 0");
return;
}

if (sizeof(dram) / sizeof(char) <= dram_p) {
iprintf("\nERROR: PTR >= RAM SIZE");
return;
}
CHECK_DRAM_P_RANGE(dram_p)

if (!dram[dram_p]) {
iprintf("\nERROR: '\\0' OUTPUT");
return;
}

iprintf("%c", dram[dram_p]);

break;

case B_GET_VAL:
CHECK_DRAM_P_RANGE(dram_p)

iprintf("\n");

s |= S_NEED_REDRAW;
Expand All @@ -132,8 +117,7 @@ void run() {
if (key_state & KEY_SELECT)
goto keyboard_interrupt;

if (key_state & KEY_START)
break;
if (key_state & KEY_START) break;

if (key_state & (KEY_A | KEY_UP)) {
++dram[dram_p];
Expand Down Expand Up @@ -167,44 +151,49 @@ void run() {
}

iprintf("\n");

break;

case B_WHILE:
if (!dram[dram_p]) {
for (;;++pram_p) {
if (sizeof(pram) / sizeof(char) <= pram_p) {
iprintf("\nERROR: FAILED TO FIND ']'");
return;
}

if (pram[pram_p] == B_WHILE) {
++block_cnt;
continue;
}

if (pram[pram_p] == B_DO_WHILE && !--block_cnt)
break;
CHECK_DRAM_P_RANGE(dram_p)

if (dram[dram_p]) break;

for (;;++pram_p) {
if (sizeof(pram) / sizeof(char) <= pram_p) {
iprintf("\nERROR: FAILED TO FIND ']'");
return;
}

if (pram[pram_p] == B_WHILE) {
++block_cnt;
continue;
}

if (pram[pram_p] == B_DO_WHILE && !--block_cnt) break;
}

break;

case B_DO_WHILE:
if (dram[dram_p]) {
for (;;--pram_p) {
if (pram_p < 0) {
iprintf("\nERROR: FAILED TO FIND '['");
return;
}

if (pram[pram_p] == B_DO_WHILE) {
++block_cnt;
continue;
}

if (pram[pram_p] == B_WHILE && !--block_cnt)
break;
CHECK_DRAM_P_RANGE(dram_p)

if (!dram[dram_p]) break;

for (;;--pram_p) {
if (pram_p < 0) {
iprintf("\nERROR: FAILED TO FIND '['");
return;
}

if (pram[pram_p] == B_DO_WHILE) {
++block_cnt;
continue;
}

if (pram[pram_p] == B_WHILE && !--block_cnt) break;
}

break;
}

Expand Down

0 comments on commit 784fb4f

Please sign in to comment.