This commit is contained in:
Tony Di Nucci 2019-04-25 22:17:31 +01:00
parent e0b7fe59f7
commit 96b694c946
7 changed files with 143 additions and 59 deletions

Binary file not shown.

View File

@ -1,49 +1,76 @@
; head position
ldx #$00
stx $00 ; head position low
ldx #$e0
stx $01 ; head position high
draw_addr = $e000
key_addr = $f001
draw_colour_addr = $10
; colours
ldx #$f0
stx $04 ; paint colour
ldx #$00
stx $05 ; rubber colour
loop
lda #$0
jsr read_key
cmp #$0
beq loop
jsr one
jsr two
jsr three
jsr four
cmp #$50
bne not_left
jsr move_left
clv
bvc loop
clv
bvc end
not_left
cmp #$4f
bne not_right
jsr move_right
clv
bvc loop
one
lda #$50
ldx #$00
sta ($00, x)
inc $00
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
two
lda #$F0
ldx #$00
sta ($00, x)
move_right
inc $00
jsr render_dot
rts
three
lda #$FF
ldx #$00
sta ($00, x)
inc $00
move_left
dec $00
jsr render_dot
rts
four
lda #$af
ldx #$00
sta ($00, x)
inc $00
move_up
lda $00
sbc #$40
sta $0
jsr render_dot
rts
end
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

43
src/machine/keyboard.cpp Normal file
View File

@ -0,0 +1,43 @@
#include "keyboard.h"
#include <SDL2/SDL.h>
#include <iostream>
using namespace std;
namespace emu_6502 {
Keyboard::Keyboard(Memory& memory) : memory(memory) {
count = 0;
}
void Keyboard::read() {
SDL_PollEvent(&event);
switch (event.type) {
case SDL_KEYDOWN:
//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;
}
}
}

26
src/machine/keyboard.h Normal file
View File

@ -0,0 +1,26 @@
#ifndef INC_6502_EMULATOR_KEYBOARD_H
#define INC_6502_EMULATOR_KEYBOARD_H
#include "memory.h"
#include <SDL2/SDL.h>
namespace emu_6502 {
class Keyboard {
private:
static const uint16_t LAST_KEY_PRESS_ADDR = 0xF001;
uint16_t count;
SDL_Event event;
Memory& memory;
public:
Keyboard(Memory& memory);
Keyboard(const Keyboard&) = delete;
Keyboard& operator=(const Keyboard&) = delete;
void read();
};
}
#endif //INC_6502_EMULATOR_KEYBOARD_H

View File

@ -1,5 +1,6 @@
#include "machine.h"
#include "terminal.h"
#include "keyboard.h"
#include "../opcode/opcode-handler-directory.h"
#include <stdexcept>
@ -7,25 +8,26 @@
namespace emu_6502 {
class MachineImpl {
private:
bool terminal_initialised = false;
uint16_t code_loaded_at;
uint16_t code_size;
uint32_t instruction_iteration;
unique_ptr<Cpu> cpu;
unique_ptr<Memory> memory;
unique_ptr<Stack> stack;
unique_ptr<Terminal> terminal;
unique_ptr<Keyboard> keyboard;
unique_ptr<OpcodeHandlerDirectory> opcode_handler_dir;
public:
MachineImpl(bool init_terminal) {
cpu = make_unique<Cpu>();
memory = make_unique<Memory>();
stack = make_unique<Stack>(*memory, cpu->get_sp());
keyboard = make_unique<Keyboard>(*memory);
if (init_terminal) {
if (init_terminal)
terminal = make_unique<Terminal>(*memory);
terminal_initialised = true;
}
opcode_handler_dir = make_unique<OpcodeHandlerDirectory>();
}
@ -77,8 +79,10 @@ namespace emu_6502 {
auto byte = read_program_byte();
opcode_handler_dir->execute(byte, machine);
// if (terminal_initialised)
// terminal->refresh();
if (instruction_iteration++ >= 100) {
keyboard->read();
instruction_iteration = 0;
}
}
}
};

View File

@ -25,19 +25,6 @@ namespace emu_6502 {
SDL_Quit();
}
// void Terminal::refresh() {
// int x, y = 0;
// for (auto i = LOW_ADDR; i <= HIGH_ADDR; i++) {
// x = (i - LOW_ADDR) % WIDTH;
// y = (i - LOW_ADDR) / HEIGHT;
//
// draw_pixel(x, y, memory.get_at(i));
// }
//
// SDL_RenderPresent(renderer);
// SDL_Delay(150);
// }
void Terminal::on_memory_written(pair<uint16_t, uint8_t> address_value) {
uint16_t address = address_value.first;
@ -49,7 +36,7 @@ namespace emu_6502 {
draw_pixel(x, y, colour);
SDL_RenderPresent(renderer);
SDL_Delay(5);
SDL_Delay(50);
}
}

View File

@ -19,7 +19,6 @@ namespace emu_6502 {
Memory& memory;
SDL_Event event;
SDL_Renderer* renderer;
SDL_Window* window;
@ -31,8 +30,6 @@ namespace emu_6502 {
Terminal(const Terminal&) = delete;
Terminal& operator=(const Terminal&) = delete;
~Terminal();
//void refresh();
};
}