mirror of
https://github.com/tdinucci/6502-emulator.git
synced 2025-02-18 14:30:29 +00:00
Keyboard
This commit is contained in:
parent
e0b7fe59f7
commit
96b694c946
BIN
sample/a.o65
BIN
sample/a.o65
Binary file not shown.
@ -1,49 +1,76 @@
|
|||||||
; head position
|
draw_addr = $e000
|
||||||
ldx #$00
|
key_addr = $f001
|
||||||
stx $00 ; head position low
|
draw_colour_addr = $10
|
||||||
ldx #$e0
|
|
||||||
stx $01 ; head position high
|
|
||||||
|
|
||||||
; colours
|
loop
|
||||||
ldx #$f0
|
lda #$0
|
||||||
stx $04 ; paint colour
|
jsr read_key
|
||||||
ldx #$00
|
cmp #$0
|
||||||
stx $05 ; rubber colour
|
beq loop
|
||||||
|
|
||||||
jsr one
|
cmp #$50
|
||||||
jsr two
|
bne not_left
|
||||||
jsr three
|
jsr move_left
|
||||||
jsr four
|
clv
|
||||||
|
bvc loop
|
||||||
|
|
||||||
clv
|
not_left
|
||||||
bvc end
|
cmp #$4f
|
||||||
|
bne not_right
|
||||||
|
jsr move_right
|
||||||
|
clv
|
||||||
|
bvc loop
|
||||||
|
|
||||||
one
|
not_right
|
||||||
lda #$50
|
cmp #$52
|
||||||
ldx #$00
|
bne not_up
|
||||||
sta ($00, x)
|
jsr move_up
|
||||||
inc $00
|
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
|
rts
|
||||||
|
|
||||||
two
|
move_right
|
||||||
lda #$F0
|
|
||||||
ldx #$00
|
|
||||||
sta ($00, x)
|
|
||||||
inc $00
|
inc $00
|
||||||
|
jsr render_dot
|
||||||
rts
|
rts
|
||||||
|
|
||||||
three
|
move_left
|
||||||
lda #$FF
|
dec $00
|
||||||
ldx #$00
|
jsr render_dot
|
||||||
sta ($00, x)
|
|
||||||
inc $00
|
|
||||||
rts
|
rts
|
||||||
|
|
||||||
four
|
move_up
|
||||||
lda #$af
|
lda $00
|
||||||
ldx #$00
|
sbc #$40
|
||||||
sta ($00, x)
|
sta $0
|
||||||
inc $00
|
jsr render_dot
|
||||||
rts
|
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
43
src/machine/keyboard.cpp
Normal 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
26
src/machine/keyboard.h
Normal 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
|
@ -1,5 +1,6 @@
|
|||||||
#include "machine.h"
|
#include "machine.h"
|
||||||
#include "terminal.h"
|
#include "terminal.h"
|
||||||
|
#include "keyboard.h"
|
||||||
#include "../opcode/opcode-handler-directory.h"
|
#include "../opcode/opcode-handler-directory.h"
|
||||||
|
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
@ -7,25 +8,26 @@
|
|||||||
namespace emu_6502 {
|
namespace emu_6502 {
|
||||||
class MachineImpl {
|
class MachineImpl {
|
||||||
private:
|
private:
|
||||||
bool terminal_initialised = false;
|
|
||||||
uint16_t code_loaded_at;
|
uint16_t code_loaded_at;
|
||||||
uint16_t code_size;
|
uint16_t code_size;
|
||||||
|
|
||||||
|
uint32_t instruction_iteration;
|
||||||
|
|
||||||
unique_ptr<Cpu> cpu;
|
unique_ptr<Cpu> cpu;
|
||||||
unique_ptr<Memory> memory;
|
unique_ptr<Memory> memory;
|
||||||
unique_ptr<Stack> stack;
|
unique_ptr<Stack> stack;
|
||||||
unique_ptr<Terminal> terminal;
|
unique_ptr<Terminal> terminal;
|
||||||
|
unique_ptr<Keyboard> keyboard;
|
||||||
unique_ptr<OpcodeHandlerDirectory> opcode_handler_dir;
|
unique_ptr<OpcodeHandlerDirectory> opcode_handler_dir;
|
||||||
public:
|
public:
|
||||||
MachineImpl(bool init_terminal) {
|
MachineImpl(bool init_terminal) {
|
||||||
cpu = make_unique<Cpu>();
|
cpu = make_unique<Cpu>();
|
||||||
memory = make_unique<Memory>();
|
memory = make_unique<Memory>();
|
||||||
stack = make_unique<Stack>(*memory, cpu->get_sp());
|
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 = make_unique<Terminal>(*memory);
|
||||||
terminal_initialised = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
opcode_handler_dir = make_unique<OpcodeHandlerDirectory>();
|
opcode_handler_dir = make_unique<OpcodeHandlerDirectory>();
|
||||||
}
|
}
|
||||||
@ -77,8 +79,10 @@ namespace emu_6502 {
|
|||||||
auto byte = read_program_byte();
|
auto byte = read_program_byte();
|
||||||
opcode_handler_dir->execute(byte, machine);
|
opcode_handler_dir->execute(byte, machine);
|
||||||
|
|
||||||
// if (terminal_initialised)
|
if (instruction_iteration++ >= 100) {
|
||||||
// terminal->refresh();
|
keyboard->read();
|
||||||
|
instruction_iteration = 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -25,19 +25,6 @@ namespace emu_6502 {
|
|||||||
SDL_Quit();
|
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) {
|
void Terminal::on_memory_written(pair<uint16_t, uint8_t> address_value) {
|
||||||
uint16_t address = address_value.first;
|
uint16_t address = address_value.first;
|
||||||
|
|
||||||
@ -49,7 +36,7 @@ namespace emu_6502 {
|
|||||||
draw_pixel(x, y, colour);
|
draw_pixel(x, y, colour);
|
||||||
|
|
||||||
SDL_RenderPresent(renderer);
|
SDL_RenderPresent(renderer);
|
||||||
SDL_Delay(5);
|
SDL_Delay(50);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -19,7 +19,6 @@ namespace emu_6502 {
|
|||||||
|
|
||||||
Memory& memory;
|
Memory& memory;
|
||||||
|
|
||||||
SDL_Event event;
|
|
||||||
SDL_Renderer* renderer;
|
SDL_Renderer* renderer;
|
||||||
SDL_Window* window;
|
SDL_Window* window;
|
||||||
|
|
||||||
@ -31,8 +30,6 @@ namespace emu_6502 {
|
|||||||
Terminal(const Terminal&) = delete;
|
Terminal(const Terminal&) = delete;
|
||||||
Terminal& operator=(const Terminal&) = delete;
|
Terminal& operator=(const Terminal&) = delete;
|
||||||
~Terminal();
|
~Terminal();
|
||||||
|
|
||||||
//void refresh();
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user