From fd260cc8406536d652a308cde31ca3136cdc294a Mon Sep 17 00:00:00 2001 From: Tony Di Nucci Date: Fri, 26 Apr 2019 02:23:50 +0100 Subject: [PATCH] Improved sample program --- sample/a.o65 | Bin 103 -> 179 bytes sample/draw.asm | 72 ------------- sample/draw.s | 171 ++++++++++++++++++++++++++++++ sample/test.s | 76 ------------- src/machine/keyboard.cpp | 22 +--- src/machine/terminal.cpp | 4 +- test/jump-opcode-handler-test.cpp | 2 +- 7 files changed, 175 insertions(+), 172 deletions(-) delete mode 100644 sample/draw.asm create mode 100644 sample/draw.s delete mode 100644 sample/test.s diff --git a/sample/a.o65 b/sample/a.o65 index 139c6d48a585684637b3be5a590e53b9fa640179..948ef91a02dbce28c96fc6432831d07b4ef05fa0 100644 GIT binary patch literal 179 zcmZ3)(8jRnK^voj728RM4?j-^Twqg(WAou)JLwN(IfGe2Kvo%;6?oyg!UPZth}JTG zSj5oB_#t5_!^!^__?I#seZPV48RH_Lfj$vz&lnU|v4Nx*F7Pa6Jo(@Q_c5^4F|hbD zh6fHC*p@Lqc<_ODDZ@*LR)!5s#~496fHJcufMni-WHNy=6PTWXWu6Hv6h literal 103 zcmZ30v*3FZF7p^J<16k*H1e{sR q_+b%4ALECFXABClYzfD}(UnvU! diff --git a/sample/draw.asm b/sample/draw.asm deleted file mode 100644 index 218cfe1..0000000 --- a/sample/draw.asm +++ /dev/null @@ -1,72 +0,0 @@ -; head position -ldx #$00 -stx $00 ; head position low -ldx #$e0 -stx $01 ; head position high - -; old head position -ldx #$ff ; start one behind head -stx $02 ; old head position low -ldx #$e0 -stx $03 ; old head position high - -; colours -ldx #$f0 -stx $04 ; paint colour -ldx #$00 -stx $05 ; rubber colour - -move_head_right - inc $00 - - lda $04 - ldx #$00 - sta ($00, x) - - clv - bvc move_rubber_right - -after_rubbed - ldx $00 - cpx #$ff - beq move_head_down - bne move_head_right - -move_rubber_right - inc $02 - - lda $05 - ldx #$02 - sta ($00, x) - -; ldx $02 -; cpx #$40 -; bne continue_rubbing -; dec $02 - clv -; bvc continue_rubbing - -;continue_rubbing - ldx $02 - cpx #$ff - beq move_rubber_down - clv - bvc after_rubbed - -move_head_down - inc $01 - - ldx $00 - stx $00 - - clv - bvc move_head_right - -move_rubber_down - inc $03 - ldx $00 - stx $02 - bvc after_rubbed - - - diff --git a/sample/draw.s b/sample/draw.s new file mode 100644 index 0000000..4265c8c --- /dev/null +++ b/sample/draw.s @@ -0,0 +1,171 @@ +; simple program that paints cycling colours to the screen using the +; keyboard curor keys +; +; The emulator loads programs at 0x600 so make sure to assemble with text segment at this address. +; Compile with xa using command: +; xa draw.s -v -bt 1536 + +; draw position +ldx #$00 +stx $00 ; draw position low +ldx #$e0 +stx $01 ; draw position high + +; buffer for last key pressed +key_addr = $f001 + +; address that holds the current colour to draw with +draw_colour_addr = $10 + +left_key_code = $50 +right_key_code = $4f +up_key_code = $52 +down_key_code = $51 + +; main program loop +loop + jsr read_key + + ; if we didn't read a key loop back round + cmp #$0 + beq loop + + cmp #left_key_code + bne not_left + jsr move_left + jmp loop + + not_left + cmp #right_key_code + bne not_right + jsr move_right + jmp loop + + not_right + cmp #up_key_code + bne not_up + jsr move_up + jmp loop + + not_up + cmp #down_key_code + bne loop + jsr move_down + jmp loop + + jmp loop + +read_key + lda key_addr + + ; clear the key press buffer so we don't read same key press twice + ldx #$0 + stx key_addr + + rts + +move_right + lda $00 + + ; if we're at $ff then we need to increment the high byte before rendering + cmp #$ff + bne do_right_render + + ; if we're at $efff then we have to stop because we'd start writing outside of video memory + lda $01 + cmp $ef + bcs after_right_render + + ; increment high byte and reset the low one + inc $01 + ldx #$00 + stx $00 + + do_right_render + inc $00 + jsr render_dot + + after_right_render + rts + +; comments for "move_right" also apply here but direction is obviously reversed +move_left + lda $00 + + cmp #$00 + bne do_left_render + + lda $01 + cmp #$e0 + beq after_left_render + + dec $01 + ldx #$00 + stx $00 + + do_left_render + dec $00 + jsr render_dot + + after_left_render + rts + +move_up + ; don't want to risk moving into non graphics memory so perform boundary checks + ldx $00 + + ; each line contains $40 (64) pixels, if low byte is above this then we're safe to move + cpx #$40 + bcs after_up_boundary_check + + ; couldn't tell if we're safe by looking at low byte, does the high byte allow move? + ldx $01 + cpx #$e0 + beq after_up_render + + after_up_boundary_check + lda $00 + sbc #$40 + sta $00 + ; if carry flag is set then we'll also have to decrement the high byte + bcs after_up_subtract + dec $01 + + after_up_subtract + jsr render_dot + + after_up_render + rts + +; comments for "move_up" also apply here but direction is obviously reversed +move_down + ldx $00 + cpx #$9b + bcc after_down_boundary_check + ldx $01 + cpx #$ef + beq after_down_render + + after_down_boundary_check + lda $00 + adc #$40 + sta $00 + bcc after_down_add + inc $01 + + after_down_add + jsr render_dot + + after_down_render + rts + +render_dot + ; change the drawing colour (256 colours and we'll wrap around when $ff is hit) + inc draw_colour_addr + lda draw_colour_addr + + ; write the "draw colour" to address pointed to across ($01 $00) - little endian + ldx #$00 + sta ($00, x) + + rts diff --git a/sample/test.s b/sample/test.s deleted file mode 100644 index a1edee9..0000000 --- a/sample/test.s +++ /dev/null @@ -1,76 +0,0 @@ -draw_addr = $e000 -key_addr = $f001 -draw_colour_addr = $10 - -loop - lda #$0 - jsr read_key - cmp #$0 - beq loop - - cmp #$50 - bne not_left - jsr move_left - clv - bvc loop - - not_left - cmp #$4f - bne not_right - jsr move_right - clv - bvc loop - - not_right - cmp #$52 - bne not_up - jsr move_up - clv - bvc loop - - not_up - cmp #$51 - bne loop - jsr move_down - clv - bvc loop - - clv - bvc loop - -read_key - lda key_addr - ldx #$0 - stx key_addr - rts - -move_right - inc $00 - jsr render_dot - rts - -move_left - dec $00 - jsr render_dot - rts - -move_up - lda $00 - sbc #$40 - sta $0 - jsr render_dot - rts - -move_down - lda $00 - adc #$3f - sta $0 - jsr render_dot - rts - -render_dot - inc draw_colour_addr - lda draw_colour_addr - ldx $00 - sta draw_addr, x - rts diff --git a/src/machine/keyboard.cpp b/src/machine/keyboard.cpp index 6726560..bce3b8e 100644 --- a/src/machine/keyboard.cpp +++ b/src/machine/keyboard.cpp @@ -1,6 +1,5 @@ #include "keyboard.h" #include -#include using namespace std; @@ -14,28 +13,11 @@ namespace emu_6502 { switch (event.type) { case SDL_KEYDOWN: - - //if(event.key.keysym.scancode < 255) + if (event.key.keysym.scancode < 255) memory.set_at(LAST_KEY_PRESS_ADDR, event.key.keysym.scancode); - cout << "CODE: " << event.key.keysym.scancode << endl; -// switch (event.key.keysym.sym) { -// case SDLK_LEFT: -// cout << "LEFT " << (uint16_t) count++ << endl; -// break; -// case SDLK_RIGHT: -// cout << "RIGHT" << (uint16_t) count++ << endl; -// break; -// case SDLK_UP: -// cout << "UP" << (uint16_t) count++ << endl; -// break; -// case SDLK_DOWN: -// cout << "DOWN" << (uint16_t) count++ << endl; -// break; -// default: -// break; -// } break; + default: return; } diff --git a/src/machine/terminal.cpp b/src/machine/terminal.cpp index af026e7..c20cf14 100644 --- a/src/machine/terminal.cpp +++ b/src/machine/terminal.cpp @@ -36,7 +36,7 @@ namespace emu_6502 { draw_pixel(x, y, colour); SDL_RenderPresent(renderer); - SDL_Delay(50); + SDL_Delay(20); } } @@ -46,8 +46,6 @@ namespace emu_6502 { green = (colour >> 2) & 0x07; blue = colour & 0x03; - //cout << "x: " << (int)x << " y: " << (int)y << " RED: " << (int)red << " GREEN: " << (int)green << " BLUE: " << (int)blue << endl; - SDL_SetRenderDrawColor(renderer, red * 36, green * 36, blue * 85, 255); SDL_Rect rect{}; rect.x = x * PIXEL_WEIGHT; diff --git a/test/jump-opcode-handler-test.cpp b/test/jump-opcode-handler-test.cpp index f91ffa5..7d2d48c 100644 --- a/test/jump-opcode-handler-test.cpp +++ b/test/jump-opcode-handler-test.cpp @@ -62,5 +62,5 @@ TEST(LoadOpcodeHandlerContainer, RTS) { } ASSERT_EQ(0x99, machine->get_cpu().get_a().get_value()); - ASSERT_EQ(0x605, machine->get_cpu().get_pc().get_value()); + ASSERT_EQ(0x604, machine->get_cpu().get_pc().get_value()); } \ No newline at end of file