Improved graphics performance (callback on memory write)

This commit is contained in:
Tony Di Nucci 2019-04-25 19:43:04 +01:00
parent 5acb3ba8bb
commit e0b7fe59f7
8 changed files with 98 additions and 15 deletions

49
sample/test.s Normal file
View File

@ -0,0 +1,49 @@
; head position
ldx #$00
stx $00 ; head position low
ldx #$e0
stx $01 ; head position high
; colours
ldx #$f0
stx $04 ; paint colour
ldx #$00
stx $05 ; rubber colour
jsr one
jsr two
jsr three
jsr four
clv
bvc end
one
lda #$50
ldx #$00
sta ($00, x)
inc $00
rts
two
lda #$F0
ldx #$00
sta ($00, x)
inc $00
rts
three
lda #$FF
ldx #$00
sta ($00, x)
inc $00
rts
four
lda #$af
ldx #$00
sta ($00, x)
inc $00
rts
end

View File

@ -77,8 +77,8 @@ namespace emu_6502 {
auto byte = read_program_byte();
opcode_handler_dir->execute(byte, machine);
if (terminal_initialised)
terminal->refresh();
// if (terminal_initialised)
// terminal->refresh();
}
}
};

View File

@ -7,6 +7,7 @@ namespace emu_6502 {
Memory::Memory() {
memory = vector<uint8_t>(0xFFFF);
on_write_callbacks = {};
}
uint8_t Memory::get_at(uint16_t address) {
@ -15,6 +16,11 @@ namespace emu_6502 {
void Memory::set_at(uint16_t address, uint8_t value) {
memory.at(address) = value;
for (auto& cb : on_write_callbacks) {
pair<uint16_t, uint8_t> address_value{address, value};
cb(address_value);
}
}
uint8_t Memory::get_from_page(uint8_t page, uint8_t offset) {
@ -24,4 +30,8 @@ namespace emu_6502 {
void Memory::set_on_page(uint8_t page, uint8_t offset, uint8_t value) {
set_at(get_page_offset(page) + offset, value);
}
void Memory::register_callback(function<void(pair<uint16_t, uint8_t>)> callback) {
on_write_callbacks.push_back(callback);
}
}

View File

@ -3,6 +3,7 @@
#include <vector>
#include <cstdint>
#include <functional>
using namespace std;
@ -10,6 +11,7 @@ namespace emu_6502 {
class Memory {
private:
vector<uint8_t> memory;
vector<function<void(pair<uint16_t, uint8_t>)>> on_write_callbacks;
uint16_t get_page_offset(uint8_t page);
@ -23,6 +25,8 @@ namespace emu_6502 {
uint8_t get_from_page(uint8_t page, uint8_t offset);
void set_on_page(uint8_t page, uint8_t offset, uint8_t value);
void register_callback(function<void(pair<uint16_t, uint8_t>)> callback);
};
}

View File

@ -15,6 +15,8 @@ namespace emu_6502 {
for (auto i = LOW_ADDR; i <= HIGH_ADDR; i++) {
memory.set_at(i, 0);
}
memory.register_callback([this](pair<uint16_t, uint8_t> addr_val) { on_memory_written(addr_val); });
}
Terminal::~Terminal() {
@ -23,17 +25,32 @@ 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;
// 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);
// }
draw_pixel(x, y, memory.get_at(i));
void Terminal::on_memory_written(pair<uint16_t, uint8_t> address_value) {
uint16_t address = address_value.first;
if (address >= LOW_ADDR && address <= HIGH_ADDR) {
uint8_t colour = address_value.second;
int x = (address - LOW_ADDR) % WIDTH;
int y = (address - LOW_ADDR) / HEIGHT;
draw_pixel(x, y, colour);
SDL_RenderPresent(renderer);
SDL_Delay(5);
}
SDL_RenderPresent(renderer);
//SDL_Delay(20);
}
void Terminal::draw_pixel(int x, int y, uint8_t colour) {

View File

@ -23,6 +23,7 @@ namespace emu_6502 {
SDL_Renderer* renderer;
SDL_Window* window;
void on_memory_written(pair<uint16_t, uint8_t> address_value);
void draw_pixel(int x, int y, uint8_t colour);
public:
@ -31,7 +32,7 @@ namespace emu_6502 {
Terminal& operator=(const Terminal&) = delete;
~Terminal();
void refresh();
//void refresh();
};
}

View File

@ -12,7 +12,9 @@ using namespace std;
using namespace emu_6502;
int main() {
ifstream in("/home/tony/CLionProjects/6502-emulator/sample/a.o65", ios::binary);
string binary = "/home/tony/CLionProjects/6502-emulator/sample/a.o65";
//string binary = "/home/tony/Downloads/6502_functional_test.bin";
ifstream in(binary, ios::binary);
if (in.fail())
throw runtime_error("Failed to read program file");
@ -21,7 +23,7 @@ int main() {
(istreambuf_iterator<char>()));
auto machine = make_unique<Machine>();
machine->load(code, 0x1000);
machine->load(code, 0x600);
machine->execute();
return 0;

View File

@ -38,7 +38,7 @@ namespace emu_6502 {
auto low_byte = machine.get_stack().pop();
auto high_byte = machine.get_stack().pop();
uint16_t return_address = (high_byte << 8) + low_byte + 1;
uint16_t return_address = (high_byte << 8) + low_byte;// + 1;
machine.get_cpu().get_pc().set_value(return_address);
}
}