From f86abbe0e6840931b870909e016c83b06bc91212 Mon Sep 17 00:00:00 2001 From: Thiago Auler Date: Mon, 20 Nov 2017 12:25:09 -0200 Subject: [PATCH 01/15] creating interface i/o functions --- src/interface.c | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 src/interface.c diff --git a/src/interface.c b/src/interface.c new file mode 100644 index 0000000..b1bcba8 --- /dev/null +++ b/src/interface.c @@ -0,0 +1,35 @@ +#include +#include "inc/interface.h" +#include "inc/memory.h" + +void io_init() +{ + initscr(); + noecho(); + cbreak(); + nodelay(stdscr, TRUE); +} + +void input() +{ + int ch = getch(); + if (ch != ERR) + { + keyboard_buffer = ch | 0x80; + keyboard_control = 0xFF; + } +} + +void output() +{ + // display is ready to ouptup + if (display_buffer & 0x80) + { + // outputs the buffer character + display_buffer = display_buffer & 0x7F; + addch(display_buffer); + //refresh(); + } + + //refresh(); +} From 82450e7b579baba06585356e07c3efc2f8e2214f Mon Sep 17 00:00:00 2001 From: Thiago Auler Date: Mon, 20 Nov 2017 12:25:34 -0200 Subject: [PATCH 02/15] creating interface i/o functions --- src/inc/interface.h | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 src/inc/interface.h diff --git a/src/inc/interface.h b/src/inc/interface.h new file mode 100644 index 0000000..0e74d2f --- /dev/null +++ b/src/inc/interface.h @@ -0,0 +1,8 @@ +#ifndef _APPLE_I_INTERFACE_H_ +#define _APPLE_I_INTERFACE_H_ + +void io_init(void); +void input(void); +void output(void); + +#endif \ No newline at end of file From 065f11ac4afa6a273a91f14eb5b34f92e3b4d2f6 Mon Sep 17 00:00:00 2001 From: Thiago Auler Date: Mon, 20 Nov 2017 12:30:55 -0200 Subject: [PATCH 03/15] creating interface i/o functions --- src/inc/opcodes.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/inc/opcodes.h b/src/inc/opcodes.h index b6990e4..3c73ac8 100644 --- a/src/inc/opcodes.h +++ b/src/inc/opcodes.h @@ -64,7 +64,7 @@ void bit(void); // test bits in memory with accumulator void bmi(void); // branch on result minus void bne(void); // branch on result not zero void bpl(void); // branch on result plus -void bkk(void); // force break +void brk(void); // force break void bvc(void); // branch on overflow clear void bvs(void); // branch on overflow set void clc(void); // clear carry flag From aff8e019ec81539499f2171d32dfe0f6674e7f7a Mon Sep 17 00:00:00 2001 From: Thiago Auler Date: Mon, 20 Nov 2017 12:32:03 -0200 Subject: [PATCH 04/15] creating interface i/o functions --- src/main.c | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/main.c b/src/main.c index 07ee6e1..877b550 100644 --- a/src/main.c +++ b/src/main.c @@ -1,11 +1,7 @@ -#include #include "inc/6502.h" int main(int argc, char **argv) { - setbuf(stdout, NULL); - printf("Apple-I Computer\n"); - init(); // start the processor run(); // and runs it From e05e5e375f27e7fbe04d3de1104d55e10885919d Mon Sep 17 00:00:00 2001 From: Thiago Auler Date: Mon, 20 Nov 2017 12:33:32 -0200 Subject: [PATCH 05/15] creating interface i/o functions --- src/memory.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/memory.c b/src/memory.c index f57f291..6c1751b 100644 --- a/src/memory.c +++ b/src/memory.c @@ -58,10 +58,10 @@ db read_byte(dw address) { return display_buffer; } - /*else if (address == 0xD013) + else if (address == 0xD013) { return display_control; - }*/ + } else if (address >= 0xFF00 && address <= 0xFFFF) { // wozmon ROM @@ -98,22 +98,22 @@ void write_mem(dw address, db data) // 4KB memory RAM ram_memory[address] = data; } - /*else if (address == 0xD010) + else if (address == 0xD010) { keyboard_buffer = data; - }*/ - /*else if (address == 0xD011) + } + else if (address == 0xD011) { keyboard_control = data; - }*/ + } else if (address == 0xD012) { display_buffer = data; } - /*else if (address == 0xD013) + else if (address == 0xD013) { display_control = data; - }*/ + } // any other addressed memory will be ignored on write } From 50a17cd13b19318b80a9a47926157492e95fb045 Mon Sep 17 00:00:00 2001 From: Thiago Auler Date: Mon, 20 Nov 2017 12:35:05 -0200 Subject: [PATCH 06/15] creating interface i/o functions --- src/opcodes.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/opcodes.c b/src/opcodes.c index 29bc5d7..c4e3f2d 100644 --- a/src/opcodes.c +++ b/src/opcodes.c @@ -261,7 +261,7 @@ void bpl() } } -void bkk() +void brk() { // force break I_SET; From 1170f3eaae3f6a96d4ae4d969092f319fc9be4d6 Mon Sep 17 00:00:00 2001 From: Thiago Auler Date: Mon, 20 Nov 2017 12:36:53 -0200 Subject: [PATCH 07/15] creating interface i/o functions --- src/6502.c | 29 +++++------------------------ 1 file changed, 5 insertions(+), 24 deletions(-) diff --git a/src/6502.c b/src/6502.c index 0b7a2f7..93cf4d8 100644 --- a/src/6502.c +++ b/src/6502.c @@ -1,5 +1,3 @@ -#include - #include "inc/types.h" #include "inc/memory.h" #include "inc/opcodes.h" @@ -9,6 +7,8 @@ oct opcode_in_table; void init() { + io_init(); + // pc is set using 0xFFFC pc = read_word(0xFFFC); sp = 0xFF; @@ -81,7 +81,7 @@ void execute() case BMI: return bmi(); case BNE: return bne(); case BPL: return bpl(); - case BRK: return bkk(); + case BRK: return brk(); case BVC: return bvc(); case BVS: return bvs(); case CLC: return clc(); @@ -139,25 +139,6 @@ void execute() } } -void display() -{ - //if (display_control == 0xA7) - { - // display is ready to ouptup - if (display_buffer & 0x80) - { - // outputs the buffer character - display_buffer = display_buffer & 0x7F; - printf("%c", display_buffer); - } - } -} - -void read_input() -{ - // verifies if the keyboard is being pressed... -} - void run() { while (1) @@ -166,8 +147,8 @@ void run() decode(); execute(); - display(); - read_input(); + input(); + output(); } } From 963694d1f90fb7689d836b2960d4f3978beb9c60 Mon Sep 17 00:00:00 2001 From: Thiago Auler Date: Mon, 20 Nov 2017 12:37:23 -0200 Subject: [PATCH 08/15] #include "inc/interface.h" --- src/6502.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/6502.c b/src/6502.c index 93cf4d8..ccb390b 100644 --- a/src/6502.c +++ b/src/6502.c @@ -1,6 +1,7 @@ #include "inc/types.h" #include "inc/memory.h" #include "inc/opcodes.h" +#include "inc/interface.h" ocl opcode_in_list; oct opcode_in_table; From b671408ce3b86491d7e0bcad865aec8d295fbf2b Mon Sep 17 00:00:00 2001 From: Thiago Auler Date: Mon, 20 Nov 2017 12:40:38 -0200 Subject: [PATCH 09/15] -lcurses --- build.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/build.sh b/build.sh index 6086375..572a774 100755 --- a/build.sh +++ b/build.sh @@ -9,7 +9,7 @@ rm -f $EXEC_NAME # build all source code for FILE in $SRC_FOLDER/*.c do - cc -g -c $FILE + cc -g -c $FILE -lcurses done # move all objects to appropriate folder @@ -17,4 +17,4 @@ mkdir -p $OBJ_FOLDER mv *.o $OBJ_FOLDER # create executable out of the objects -cc -g -o $EXEC_NAME $OBJ_FOLDER/*.o +cc -g -o $EXEC_NAME $OBJ_FOLDER/*.o -lcurses From f3d4cd7771c3df915bfe44599e4c098142ebf9ba Mon Sep 17 00:00:00 2001 From: Thiago Auler Date: Mon, 20 Nov 2017 12:43:26 -0200 Subject: [PATCH 10/15] adding -lcurses for compiling with curses lib --- build.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.sh b/build.sh index 572a774..8242c24 100755 --- a/build.sh +++ b/build.sh @@ -9,7 +9,7 @@ rm -f $EXEC_NAME # build all source code for FILE in $SRC_FOLDER/*.c do - cc -g -c $FILE -lcurses + cc -g -c $FILE done # move all objects to appropriate folder From 7003d3f63b86a1c6209117cd2ca796e5ec60ea1b Mon Sep 17 00:00:00 2001 From: Thiago Auler Date: Tue, 21 Nov 2017 15:02:06 -0200 Subject: [PATCH 11/15] adjusting i/o --- src/memory.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/memory.c b/src/memory.c index 6c1751b..75fcaf8 100644 --- a/src/memory.c +++ b/src/memory.c @@ -48,6 +48,7 @@ db read_byte(dw address) } else if (address == 0xD010) { + keyboard_control = 0x00; return keyboard_buffer; } else if (address == 0xD011) From dd4a315e937fe7ea47d1f83f8e5f4441f1f9778a Mon Sep 17 00:00:00 2001 From: Thiago Auler Date: Tue, 21 Nov 2017 15:06:40 -0200 Subject: [PATCH 12/15] ajusting i/o --- src/interface.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/interface.c b/src/interface.c index b1bcba8..7019243 100644 --- a/src/interface.c +++ b/src/interface.c @@ -8,12 +8,15 @@ void io_init() noecho(); cbreak(); nodelay(stdscr, TRUE); + + keyboard_control = 0x00; } void input() { int ch = getch(); - if (ch != ERR) + if (ch == '\n') { ch = '\r'; } + if (ch == '\r' || (ch >= '0' && ch <= '9')) { keyboard_buffer = ch | 0x80; keyboard_control = 0xFF; @@ -27,9 +30,10 @@ void output() { // outputs the buffer character display_buffer = display_buffer & 0x7F; + if (display_buffer == '\r') + { + display_buffer = '\n'; + } addch(display_buffer); - //refresh(); } - - //refresh(); } From 258a9f669222da33f090060bc3502c7c391d5da5 Mon Sep 17 00:00:00 2001 From: Thiago Auler Date: Wed, 22 Nov 2017 13:18:54 -0200 Subject: [PATCH 13/15] correcting adder/subtractor --- src/opcodes.c | 26 +++++++++----------------- 1 file changed, 9 insertions(+), 17 deletions(-) diff --git a/src/opcodes.c b/src/opcodes.c index c4e3f2d..0f07353 100644 --- a/src/opcodes.c +++ b/src/opcodes.c @@ -102,13 +102,13 @@ void adjustNZ(db r) db adder(db a, db b) { - db r = a + b; - db c = (a + b) >> 8; + db r = a + b + C_IS_SET; + db c = (a + b + C_IS_SET) >> 8; - a = a & 0b01111111; - b = b & 0b01111111; - db cc = (a + b) >> 7; - db v = c ^ cc; + a = a & 0x7F; + b = b & 0x7F; + db z = (a + b + C_IS_SET) >> 7; + db v = c ^ z; if (c == 1) { C_SET; } else { C_UNSET;} if (v == 1) { V_SET; } else { V_UNSET; } @@ -118,15 +118,6 @@ db adder(db a, db b) return r; } -db subtractor(db a, db b) -{ - // negate b operand using 2's complement - b = b ^ 0xFF; - b = b + 1; - - return adder(a, b); -} - void push_byte(db data) { write_mem(sp, data); @@ -161,7 +152,7 @@ void adc() { // add memory to accumulator with carry fetch_operand(); - ac = adder(ac, operand + C_IS_SET); + ac = adder(ac, operand); } void and() @@ -557,7 +548,8 @@ void sbc() { // subtract memory from accumulator with borrow fetch_operand(); - ac = subtractor(ac, operand - C_IS_SET); + operand = operand ^ 0xFF; + ac = adder(ac, operand); } void sec() From bac269c9c2166569210f3ff29906a5bf817e479f Mon Sep 17 00:00:00 2001 From: Thiago Auler dos Santos Date: Wed, 22 Nov 2017 19:47:01 -0200 Subject: [PATCH 14/15] correcting subtraction --- src/opcodes.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/opcodes.c b/src/opcodes.c index 0f07353..ee3fb74 100644 --- a/src/opcodes.c +++ b/src/opcodes.c @@ -308,21 +308,24 @@ void cmp() { // compare memory with accumulator fetch_operand(); - subtractor(ac, operand); + operand = operand ^ 0xFF; + adder(ac, operand); } void cpx() { // compare memory and index x fetch_operand(); - subtractor(x, operand); + operand = operand ^ 0xFF; + adder(x, operand); } void cpy() { // compare memory and index y fetch_operand(); - subtractor(y, operand); + operand = operand ^ 0xFF; + adder(y, operand); } void dec() From 1deb00495d2b7ab19dc29ed2b25c284e56acc85b Mon Sep 17 00:00:00 2001 From: Thiago Auler dos Santos Date: Wed, 22 Nov 2017 23:28:49 -0200 Subject: [PATCH 15/15] accepting numbers, text, and other symbols as user input --- src/interface.c | 4 +++- src/opcodes.c | 7 +++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/interface.c b/src/interface.c index 7019243..450facb 100644 --- a/src/interface.c +++ b/src/interface.c @@ -16,7 +16,9 @@ void input() { int ch = getch(); if (ch == '\n') { ch = '\r'; } - if (ch == '\r' || (ch >= '0' && ch <= '9')) + if (ch == '\r' || ch == '.' || ch == ':' || + (ch >= '0' && ch <= '9') || + (ch >= 'A' && ch <= 'Z')) { keyboard_buffer = ch | 0x80; keyboard_control = 0xFF; diff --git a/src/opcodes.c b/src/opcodes.c index ee3fb74..45a05cd 100644 --- a/src/opcodes.c +++ b/src/opcodes.c @@ -95,7 +95,7 @@ void fetch_operand() void adjustNZ(db r) { - if (r == 0) { Z_SET; } else { Z_UNSET;} + if (r == 0) { Z_SET; } else { Z_UNSET; } r = r >> 7; if (r == 1) { N_SET; } else { N_UNSET; } } @@ -110,7 +110,7 @@ db adder(db a, db b) db z = (a + b + C_IS_SET) >> 7; db v = c ^ z; - if (c == 1) { C_SET; } else { C_UNSET;} + if (c == 1) { C_SET; } else { C_UNSET; } if (v == 1) { V_SET; } else { V_UNSET; } adjustNZ(r); @@ -307,6 +307,7 @@ void clv() void cmp() { // compare memory with accumulator + C_SET; fetch_operand(); operand = operand ^ 0xFF; adder(ac, operand); @@ -315,6 +316,7 @@ void cmp() void cpx() { // compare memory and index x + C_SET; fetch_operand(); operand = operand ^ 0xFF; adder(x, operand); @@ -323,6 +325,7 @@ void cpx() void cpy() { // compare memory and index y + C_SET; fetch_operand(); operand = operand ^ 0xFF; adder(y, operand);