From 3029d54edf8baabb2841f9a6f3d88bfc993ae3e8 Mon Sep 17 00:00:00 2001 From: mrb0nk500 Date: Thu, 19 Dec 2019 01:02:09 -0500 Subject: Added the B register, which is used for storing the remainder during a DIV instruction. I also added the TAB, and TBA instructions, for transfering between the Accumulator, and the B register. --- asmmon.c | 132 ++++++++++++++++++++++++++++++----------------------------- opcode.h | 6 ++- sux.c | 16 ++++++-- test/input.s | 47 ++++++++++++++++++++- 4 files changed, 130 insertions(+), 71 deletions(-) diff --git a/asmmon.c b/asmmon.c index 422d8ce..da52920 100644 --- a/asmmon.c +++ b/asmmon.c @@ -4,7 +4,7 @@ #define debug 1 -#define OPNUM 89 +#define OPNUM 91 #define SETOP(num, _mne, _IMM, _ZM, _ZMX, _ZMY, _ABS, _IMPL) \ {opcodes[num].mnemonic[3] = '\0'; strncpy(opcodes[num].mnemonic, _mne, 3); \ opcodes[num].imm = _IMM; \ @@ -151,70 +151,72 @@ int asmmon(const char *fn) { SETOP(22, "ANX", 0x24, 0x84, 0x00, 0x00, 0x54, 0x00); SETOP(23, "AAX", 0x00, 0x00, 0x00, 0x00, 0x00, 0x25); SETOP(24, "STT", 0x28, 0x00, 0x00, 0x00, 0x00, 0x00); - SETOP(25, "TSX", 0x00, 0x00, 0x00, 0x00, 0x00, 0x2E); - SETOP(26, "BPO", 0x00, 0x00, 0x00, 0x00, 0x30, 0x00); - SETOP(27, "ORA", 0x31, 0x3B, 0x00, 0x00, 0x39, 0x00); - SETOP(28, "ORY", 0x32, 0x00, 0x00, 0x00, 0x62, 0x00); - SETOP(29, "OAY", 0x00, 0x00, 0x00, 0x00, 0x00, 0x33); - SETOP(23, "ORX", 0x34, 0x94, 0x00, 0x00, 0x64, 0x00); - SETOP(31, "OAX", 0x00, 0x00, 0x00, 0x00, 0x00, 0x35); - SETOP(32, "SEI", 0x00, 0x00, 0x00, 0x00, 0x00, 0x38); - SETOP(33, "TXS", 0x3E, 0x00, 0x00, 0x00, 0x00, 0x00); - SETOP(34, "BNG", 0x00, 0x00, 0x00, 0x00, 0x40, 0x00); - SETOP(35, "XOR", 0x41, 0x4B, 0x00, 0x00, 0x49, 0x00); - SETOP(36, "XRY", 0x42, 0xA2, 0x00, 0x00, 0x72, 0x00); - SETOP(37, "XAY", 0x00, 0x00, 0x00, 0x00, 0x00, 0x43); - SETOP(38, "XRX", 0x44, 0xA4, 0x00, 0x00, 0x74, 0x00); - SETOP(39, "XAX", 0x00, 0x00, 0x00, 0x00, 0x00, 0x45); - SETOP(40, "CLI", 0x00, 0x00, 0x00, 0x00, 0x00, 0x48); - SETOP(41, "BCS", 0x00, 0x00, 0x00, 0x00, 0x50, 0x00); - SETOP(42, "LSL", 0x51, 0x55, 0x00, 0x00, 0x53, 0x00); - SETOP(43, "SEC", 0x00, 0x00, 0x00, 0x00, 0x00, 0x58); - SETOP(44, "STA", 0x00, 0x7B, 0x8B, 0x9B, 0x5B, 0x00); - SETOP(45, "STY", 0x00, 0x7D, 0x8D, 0x00, 0x5D, 0x00); - SETOP(46, "STX", 0x00, 0x7E, 0x00, 0x9E, 0x5E, 0x00); - SETOP(47, "BCC", 0x00, 0x00, 0x00, 0x00, 0x60, 0x00); - SETOP(48, "LSR", 0x61, 0x65, 0x00, 0x00, 0x63, 0x00); - SETOP(49, "CLC", 0x00, 0x00, 0x00, 0x00, 0x00, 0x68); - SETOP(50, "LDA", 0x69, 0x79, 0x89, 0x99, 0x59, 0x00); - SETOP(51, "LDY", 0x6A, 0x7A, 0x8A, 0x00, 0x5A, 0x00); - SETOP(52, "LDX", 0x6C, 0x7C, 0x00, 0x9C, 0x5C, 0x00); - SETOP(53, "BEQ", 0x00, 0x00, 0x00, 0x00, 0x70, 0x00); - SETOP(54, "ROL", 0x71, 0x75, 0x00, 0x00, 0x73, 0x00); - SETOP(55, "SSP", 0x00, 0x00, 0x00, 0x00, 0x00, 0x78); - SETOP(56, "BNE", 0x00, 0x00, 0x00, 0x00, 0x80, 0x00); - SETOP(57, "ROR", 0x81, 0x85, 0x00, 0x00, 0x83, 0x00); - SETOP(58, "CSP", 0x00, 0x00, 0x00, 0x00, 0x00, 0x88); - SETOP(59, "BVS", 0x00, 0x00, 0x00, 0x00, 0x90, 0x00); - SETOP(60, "MUL", 0x91, 0x95, 0x00, 0x00, 0x93, 0x00); - SETOP(61, "SEV", 0x00, 0x00, 0x00, 0x00, 0x00, 0x98); - SETOP(62, "BVC", 0x00, 0x00, 0x00, 0x00, 0xA0, 0x00); - SETOP(63, "DIV", 0xA1, 0xA5, 0x00, 0x00, 0xA3, 0x00); - SETOP(64, "CLV", 0x00, 0x00, 0x00, 0x00, 0x00, 0xA8); - SETOP(65, "ASR", 0xA9, 0xAD, 0x00, 0x00, 0xAB, 0x00); - SETOP(66, "RTS", 0x00, 0x00, 0x00, 0x00, 0x00, 0xB0); - SETOP(67, "CMP", 0xB1, 0xF5, 0x00, 0x00, 0xE5, 0x00); - SETOP(68, "CPY", 0xB2, 0xF2, 0x00, 0x00, 0xE2, 0x00); - SETOP(69, "CAY", 0x00, 0x00, 0x00, 0x00, 0x00, 0xB3); - SETOP(70, "CPX", 0xB4, 0xF4, 0x00, 0x00, 0xE4, 0x00); - SETOP(71, "CAX", 0x00, 0x00, 0x00, 0x00, 0x00, 0xB5); - SETOP(72, "ENT", 0xB8, 0x00, 0x00, 0x00, 0x00, 0x00); - SETOP(73, "RTI", 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0); - SETOP(74, "INC", 0x00, 0xE3, 0x00, 0x00, 0xE1, 0xC1); - SETOP(75, "INY", 0x00, 0x00, 0x00, 0x00, 0x00, 0xC2); - SETOP(76, "IAY", 0x00, 0x00, 0x00, 0x00, 0x00, 0xC3); - SETOP(77, "INX", 0x00, 0x00, 0x00, 0x00, 0x00, 0xC4); - SETOP(78, "IAX", 0x00, 0x00, 0x00, 0x00, 0x00, 0xC5); - SETOP(79, "DEC", 0x00, 0x00, 0x00, 0x00, 0x00, 0xD1); - SETOP(80, "DEY", 0x00, 0x00, 0x00, 0x00, 0x00, 0xD2); - SETOP(81, "DAY", 0x00, 0xF3, 0x00, 0x00, 0xF1, 0xD3); - SETOP(82, "DEX", 0x00, 0x00, 0x00, 0x00, 0x00, 0xD4); - SETOP(83, "DAX", 0x00, 0x00, 0x00, 0x00, 0x00, 0xD5); - SETOP(84, "WAI", 0x00, 0x00, 0x00, 0x00, 0x00, 0xD8); - SETOP(85, "JSL", 0x00, 0x00, 0x00, 0x00, 0xE0, 0x00); - SETOP(86, "NOP", 0x00, 0x00, 0x00, 0x00, 0x00, 0xE8); - SETOP(87, "RTL", 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0); - SETOP(88, "BRK", 0x00, 0x00, 0x00, 0x00, 0x00, 0xF8); + SETOP(25, "TAB", 0x00, 0x00, 0x00, 0x00, 0x00, 0x2C); + SETOP(26, "TSX", 0x00, 0x00, 0x00, 0x00, 0x00, 0x2E); + SETOP(27, "BPO", 0x00, 0x00, 0x00, 0x00, 0x30, 0x00); + SETOP(28, "ORA", 0x31, 0x3B, 0x00, 0x00, 0x39, 0x00); + SETOP(29, "ORY", 0x32, 0x00, 0x00, 0x00, 0x62, 0x00); + SETOP(23, "OAY", 0x00, 0x00, 0x00, 0x00, 0x00, 0x33); + SETOP(31, "ORX", 0x34, 0x94, 0x00, 0x00, 0x64, 0x00); + SETOP(32, "OAX", 0x00, 0x00, 0x00, 0x00, 0x00, 0x35); + SETOP(33, "SEI", 0x00, 0x00, 0x00, 0x00, 0x00, 0x38); + SETOP(34, "TBA", 0x00, 0x00, 0x00, 0x00, 0x00, 0x3C); + SETOP(35, "TXS", 0x3E, 0x00, 0x00, 0x00, 0x00, 0x00); + SETOP(36, "BNG", 0x00, 0x00, 0x00, 0x00, 0x40, 0x00); + SETOP(37, "XOR", 0x41, 0x4B, 0x00, 0x00, 0x49, 0x00); + SETOP(38, "XRY", 0x42, 0xA2, 0x00, 0x00, 0x72, 0x00); + SETOP(39, "XAY", 0x00, 0x00, 0x00, 0x00, 0x00, 0x43); + SETOP(40, "XRX", 0x44, 0xA4, 0x00, 0x00, 0x74, 0x00); + SETOP(41, "XAX", 0x00, 0x00, 0x00, 0x00, 0x00, 0x45); + SETOP(42, "CLI", 0x00, 0x00, 0x00, 0x00, 0x00, 0x48); + SETOP(43, "BCS", 0x00, 0x00, 0x00, 0x00, 0x50, 0x00); + SETOP(44, "LSL", 0x51, 0x55, 0x00, 0x00, 0x53, 0x00); + SETOP(45, "SEC", 0x00, 0x00, 0x00, 0x00, 0x00, 0x58); + SETOP(46, "STA", 0x00, 0x7B, 0x8B, 0x9B, 0x5B, 0x00); + SETOP(47, "STY", 0x00, 0x7D, 0x8D, 0x00, 0x5D, 0x00); + SETOP(48, "STX", 0x00, 0x7E, 0x00, 0x9E, 0x5E, 0x00); + SETOP(49, "BCC", 0x00, 0x00, 0x00, 0x00, 0x60, 0x00); + SETOP(50, "LSR", 0x61, 0x65, 0x00, 0x00, 0x63, 0x00); + SETOP(51, "CLC", 0x00, 0x00, 0x00, 0x00, 0x00, 0x68); + SETOP(52, "LDA", 0x69, 0x79, 0x89, 0x99, 0x59, 0x00); + SETOP(53, "LDY", 0x6A, 0x7A, 0x8A, 0x00, 0x5A, 0x00); + SETOP(54, "LDX", 0x6C, 0x7C, 0x00, 0x9C, 0x5C, 0x00); + SETOP(55, "BEQ", 0x00, 0x00, 0x00, 0x00, 0x70, 0x00); + SETOP(56, "ROL", 0x71, 0x75, 0x00, 0x00, 0x73, 0x00); + SETOP(57, "SSP", 0x00, 0x00, 0x00, 0x00, 0x00, 0x78); + SETOP(58, "BNE", 0x00, 0x00, 0x00, 0x00, 0x80, 0x00); + SETOP(59, "ROR", 0x81, 0x85, 0x00, 0x00, 0x83, 0x00); + SETOP(60, "CSP", 0x00, 0x00, 0x00, 0x00, 0x00, 0x88); + SETOP(61, "BVS", 0x00, 0x00, 0x00, 0x00, 0x90, 0x00); + SETOP(62, "MUL", 0x91, 0x95, 0x00, 0x00, 0x93, 0x00); + SETOP(63, "SEV", 0x00, 0x00, 0x00, 0x00, 0x00, 0x98); + SETOP(64, "BVC", 0x00, 0x00, 0x00, 0x00, 0xA0, 0x00); + SETOP(65, "DIV", 0xA1, 0xA5, 0x00, 0x00, 0xA3, 0x00); + SETOP(66, "CLV", 0x00, 0x00, 0x00, 0x00, 0x00, 0xA8); + SETOP(67, "ASR", 0xA9, 0xAD, 0x00, 0x00, 0xAB, 0x00); + SETOP(68, "RTS", 0x00, 0x00, 0x00, 0x00, 0x00, 0xB0); + SETOP(69, "CMP", 0xB1, 0xF5, 0x00, 0x00, 0xE5, 0x00); + SETOP(70, "CPY", 0xB2, 0xF2, 0x00, 0x00, 0xE2, 0x00); + SETOP(71, "CAY", 0x00, 0x00, 0x00, 0x00, 0x00, 0xB3); + SETOP(72, "CPX", 0xB4, 0xF4, 0x00, 0x00, 0xE4, 0x00); + SETOP(73, "CAX", 0x00, 0x00, 0x00, 0x00, 0x00, 0xB5); + SETOP(74, "ENT", 0xB8, 0x00, 0x00, 0x00, 0x00, 0x00); + SETOP(75, "RTI", 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0); + SETOP(76, "INC", 0x00, 0xE3, 0x00, 0x00, 0xE1, 0xC1); + SETOP(77, "INY", 0x00, 0x00, 0x00, 0x00, 0x00, 0xC2); + SETOP(78, "IAY", 0x00, 0x00, 0x00, 0x00, 0x00, 0xC3); + SETOP(79, "INX", 0x00, 0x00, 0x00, 0x00, 0x00, 0xC4); + SETOP(80, "IAX", 0x00, 0x00, 0x00, 0x00, 0x00, 0xC5); + SETOP(81, "DEC", 0x00, 0x00, 0x00, 0x00, 0x00, 0xD1); + SETOP(82, "DEY", 0x00, 0x00, 0x00, 0x00, 0x00, 0xD2); + SETOP(83, "DAY", 0x00, 0xF3, 0x00, 0x00, 0xF1, 0xD3); + SETOP(84, "DEX", 0x00, 0x00, 0x00, 0x00, 0x00, 0xD4); + SETOP(85, "DAX", 0x00, 0x00, 0x00, 0x00, 0x00, 0xD5); + SETOP(86, "WAI", 0x00, 0x00, 0x00, 0x00, 0x00, 0xD8); + SETOP(87, "JSL", 0x00, 0x00, 0x00, 0x00, 0xE0, 0x00); + SETOP(88, "NOP", 0x00, 0x00, 0x00, 0x00, 0x00, 0xE8); + SETOP(89, "RTL", 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0); + SETOP(90, "BRK", 0x00, 0x00, 0x00, 0x00, 0x00, 0xF8); FILE *fp; if (strcasecmp(fn, "stdin") != 0) { fp = fopen(fn, "r"); diff --git a/opcode.h b/opcode.h index a759ff8..a3775db 100644 --- a/opcode.h +++ b/opcode.h @@ -29,6 +29,7 @@ #define ANX 0x24 /* bitwise ANd with X register. */ #define AAX 0x25 /* bitwise And with Accumulator, and X register. */ #define STT 0x28 /* STart Threads. */ +#define TAB 0x2C /* Transfer Accumulator to B. */ #define TSX 0x2E /* Transfer Stack pointer to X. */ #define BPO 0x30 /* Branch if POsitive. */ #define ORA 0x31 /* bitwise OR with Accumulator. */ @@ -37,6 +38,7 @@ #define ORX 0x34 /* bitwise OR with X register. */ #define OAX 0x35 /* bitwise Or with Accumulator, and X register. */ #define SEI 0x38 /* SEt Interupt flag. */ +#define TBA 0x3C /* Transfer B to Accumulator. */ #define TXS 0x3E /* Transfer X to Stack pointer. */ #define BNG 0x40 /* Branch if NeGative. */ #define XOR 0x41 /* bitwise XOR with accumulator. */ @@ -108,7 +110,7 @@ uint8_t ibcount; /* Number of bytes taken up by instruction. */ struct sux { uint64_t ps; /* The processor status register. */ - uint64_t a[8], y[8], x[8]; /* Registers A, X, and Y. */ + uint64_t a[8], b[8], y[8], x[8]; /* Registers A, B, X, and Y. */ uint64_t pc[8]; /* Program counter. */ uint16_t sp[8]; /* Stack pointer. */ uint16_t stk_st[8]; /* Starting address of each threads stack. */ @@ -160,6 +162,7 @@ static const char *opname[0x100] = { OPNAME(TSX), [0x29] = "AND a", [0x2B] = "AND zm", + OPNAME(TAB), OPNAME(BPO), [ORA] = "ORA #", [ORY] = "ORY #", @@ -170,6 +173,7 @@ static const char *opname[0x100] = { OPNAME(TXS), [0x39] = "ORA a", [0x3B] = "ORA zm", + OPNAME(TBA), OPNAME(BNG), [XOR] = "XOR #", [XRY] = "XRY #", diff --git a/sux.c b/sux.c index 70b8a92..96068a8 100644 --- a/sux.c +++ b/sux.c @@ -4,8 +4,8 @@ #include #include #define bench 0 -#define debug 0 -#define IO 1 +#define debug 1 +#define IO 0 #define keypoll 0 #if bench #include @@ -222,7 +222,9 @@ void *run(void *args) { case TYA: /* Transfer Y to Accumulator. */ case TXA: /* Transfer X to Accumulator. */ case TXY: /* Transfer X to Y. */ + case TAB: /* Transfer Accumulator to B. */ case TSX: /* Transfer Stack pointer to X. */ + case TBA: /* Transfer B to Accumulator. */ case TXS: /* Transfer X to Stack pointer. */ if (opcode == TAY) cpu->y[thread] = cpu->a[thread]; @@ -236,10 +238,17 @@ void *run(void *args) { cpu->a[thread] = cpu->x[thread]; if (opcode == TXY) cpu->y[thread] = cpu->x[thread]; + if (opcode == TAB) { + cpu->b[thread] = cpu->a[thread]; + cpu->z[thread] = (cpu->b[thread] == 0); + cpu->n[thread] = (cpu->b[thread] >> 63); + } if (opcode == TSX) { cpu->x[thread] = cpu->sp[thread] & 0xFFFF; cpu->x[thread] = cpu->stk_st[thread] << 16; } + if (opcode == TBA) + cpu->a[thread] = cpu->b[thread]; if (opcode == TXS) { cpu->sp[thread] = cpu->x[thread]; if (prefix == 0x17 && (value == thread+1 || value > 8)) { @@ -248,7 +257,7 @@ void *run(void *args) { cpu->pc[thread]+=2; } } - if (opcode == TYA || opcode == TXA) { + if (opcode == TYA || opcode == TXA || opcode == TBA) { cpu->z[thread] = (cpu->a[thread] == 0); cpu->n[thread] = (cpu->a[thread] >> 63); } @@ -1207,6 +1216,7 @@ void *run(void *args) { value += (uint64_t)addr[address+6] << 48; value += (uint64_t)addr[address+7] << 56; } + cpu->b[thread] = cpu->a[thread] % value; sum = cpu->a[thread]/value; cpu->a[thread] = sum; cpu->z[thread] = (sum == 0); diff --git a/test/input.s b/test/input.s index a2725f6..95b4077 100644 --- a/test/input.s +++ b/test/input.s @@ -12,10 +12,15 @@ string2: .byte "You typed, " end: .byte $0 -x: +scr_row: .word $0 -tmp: +scr_col: .word $0 +a: + .word $0 +b: + .word $0 + ; Input buffer. .org $2000 buffer: @@ -93,6 +98,10 @@ esc: cmp #$44 ; Did the user press the left arrow? beq left ; Yes, so move the cursor left. up: + lda scr_row + beq rset_a + dec scr_row + jsr update_pos lda #$1B sta $C001 lda #$5B @@ -101,6 +110,8 @@ up: sta $C001 jmp rset_a down: + inc scr_row + jsr update_pos lda #$1B sta $C001 lda #$5B @@ -109,6 +120,11 @@ down: sta $C001 jmp rset_a right: + lda scr_col + cmp #$50 + beq rset_a + inc scr_col + jsr update_pos lda #$1B sta $C001 lda #$5B @@ -117,6 +133,10 @@ right: sta $C001 jmp rset_a left: + lda scr_col + beq rset_a + dec scr_col + jsr update_pos lda #$1B sta $C001 lda #$5B @@ -125,8 +145,30 @@ left: sta $C001 jmp rset_a +update_pos: + lda #$1B + sta $C001 + lda #$5B + sta $C001 + lda scr_row + jsr uint_to_bcd + jmp spin + +uint_to_bcd: + div #$A + lsl #$4 + sta a + tba + sta b + lda a + ora b + rts + nl: + lda #$A sta $C001 + inc scr_row + jsr update_pos lda #$0 ; Replace newline with a null terminator. sta buffer, y ; Store said terminator into the input buffer. ldy.w #$0 ; Reset y, to print the result. @@ -189,3 +231,4 @@ viewmem viewmem ;q done + -- cgit v1.2.3-13-gbd6f