Start of graphics support

This commit is contained in:
Tony Di Nucci 2019-04-23 21:19:00 +01:00
parent af0099caa6
commit 737b50e211
8 changed files with 94 additions and 43 deletions

View File

@ -23,11 +23,16 @@ add_subdirectory(${CMAKE_CURRENT_BINARY_DIR}/googletest-src
${CMAKE_CURRENT_BINARY_DIR}/googletest-build
EXCLUDE_FROM_ALL)
find_package(SDL2 REQUIRED)
include_directories(${SDL2_INCLUDE_DIRS})
file(GLOB_RECURSE test_src "test/**.h" "test/**.cpp" "src/**.h" "src/**.cpp")
list(FILTER test_src EXCLUDE REGEX ".*/6502-emulator/src/main.cpp$")
add_executable(6502_emulator_test ${test_src})
target_link_libraries(6502_emulator_test gtest_main)
target_link_libraries(6502_emulator_test ${SDL2_LIBRARIES})
add_test(NAME run_tests COMMAND 6502_emulator_test)
file(GLOB_RECURSE emulator_src "src/**.h" "src/**.cpp")
add_executable(6502_emulator ${emulator_src})
add_executable(6502_emulator ${emulator_src})
target_link_libraries(6502_emulator ${SDL2_LIBRARIES})

9
sample/draw.asm Normal file
View File

@ -0,0 +1,9 @@
lda #$0
loop
sta $e000, x
inx
adc #$1
cpx #$ff
bne loop

View File

@ -1,4 +1,5 @@
#include "machine.h"
#include "terminal.h"
#include "../opcode/opcode-handler-directory.h"
#include <stdexcept>
@ -6,18 +7,26 @@
namespace emu_6502 {
class MachineImpl {
private:
bool terminal_initialised = false;
uint16_t code_loaded_at;
uint16_t code_size;
unique_ptr<Cpu> cpu;
unique_ptr<Memory> memory;
unique_ptr<Stack> stack;
unique_ptr<Terminal> terminal;
unique_ptr<OpcodeHandlerDirectory> opcode_handler_dir;
public:
MachineImpl() {
MachineImpl(bool init_terminal) {
cpu = make_unique<Cpu>();
memory = make_unique<Memory>();
stack = make_unique<Stack>(*memory, cpu->get_sp());
if (init_terminal) {
terminal = make_unique<Terminal>(*memory);
terminal_initialised = true;
}
opcode_handler_dir = make_unique<OpcodeHandlerDirectory>();
}
@ -67,11 +76,14 @@ namespace emu_6502 {
while (!is_eop()) {
auto byte = read_program_byte();
opcode_handler_dir->execute(byte, machine);
if (terminal_initialised)
terminal->refresh();
}
}
};
Machine::Machine() : pimpl(make_unique<MachineImpl>()) {
Machine::Machine(bool init_terminal) : pimpl(make_unique<MachineImpl>(init_terminal)) {
}
Machine::~Machine() = default;

View File

@ -17,7 +17,7 @@ namespace emu_6502 {
private:
unique_ptr<MachineImpl> pimpl;
public:
Machine();
Machine(bool init_terminal = true);
~Machine();
Machine(const Machine&) = delete;
Machine& operator=(const Machine&) = delete;

View File

@ -5,33 +5,55 @@
namespace emu_6502 {
Terminal::Terminal(Memory& memory) : memory(memory) {
// for (auto i = 0; i < 100; i++)
// memory->set_at(Terminal::LOW_ADDR + i, 0x20 + i);
//
// if ((mainwin = initscr()) == NULL) {
// fprintf(stderr, "Error initialising ncurses.\n");
// exit(EXIT_FAILURE);
// }
//
// start_color();
int width = WIDTH * PIXEL_WEIGHT;
int height = HEIGHT * PIXEL_WEIGHT;
SDL_Init(SDL_INIT_VIDEO);
SDL_CreateWindowAndRenderer(width, height, 0, &window, &renderer);
SDL_RenderClear(renderer);
for (auto i = LOW_ADDR; i <= HIGH_ADDR; i++) {
memory.set_at(i, 0);
}
}
Terminal::~Terminal() {
// delwin(mainwin);
// endwin();
cout << "shutting down" << endl;
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
}
void Terminal::refresh() {
// for (auto n = 0; n <= 100; n++) {
// auto line = (n / TERM_COLS);
// auto col = n % TERM_COLS;
//
// mvaddch(line, col, memory->get_at(Terminal::LOW_ADDR + n));
// n++;
// }
//
// wrefresh(mainwin);
int x, y = 0;
for (auto i = LOW_ADDR; i <= HIGH_ADDR; i++) {
x = (i - LOW_ADDR) % WIDTH;
y = (i - LOW_ADDR) / HEIGHT;
auto colour = memory.get_at(i);
if (colour) {
draw_pixel(x, y, colour);
}
}
SDL_RenderPresent(renderer);
SDL_Delay(5);
}
void Terminal::draw_pixel(int x, int y, uint8_t colour) {
uint8_t red, green, blue = 0;
red = colour >> 5;
green = (colour >> 2) & 0x07;
blue = colour & 0x03;
cout << "RED: " << (int)red << " GREEN: " << (int)green << " BLUE: " << (int)blue << endl;
SDL_SetRenderDrawColor(renderer, red * 36, green * 36, blue * 85, 0);
for (int i = 0; i < PIXEL_WEIGHT; i++) {
SDL_RenderDrawPoint(renderer, (x * PIXEL_WEIGHT) + i, (y * PIXEL_WEIGHT));
for (int j = 0; j < PIXEL_WEIGHT; j++) {
SDL_RenderDrawPoint(renderer, (x * PIXEL_WEIGHT) + i, (y * PIXEL_WEIGHT) + j);
}
}
}
}

View File

@ -1,8 +1,8 @@
#ifndef INC_6502_EMULATOR_TERMINAL_H
#define INC_6502_EMULATOR_TERMINAL_H
#include <ncurses.h>
#include <memory>
#include <SDL2/SDL.h>
#include "memory.h"
using namespace std;
@ -10,15 +10,20 @@ using namespace std;
namespace emu_6502 {
class Terminal {
private:
const uint16_t LOW_ADDR = 0xFB00;
const uint16_t HIGH_ADDR = 0xFF00;
const uint8_t TERM_LINES = 25;
const uint8_t TERM_COLS = 40;
//WINDOW * mainwin;
const uint16_t LOW_ADDR = 0xE000;
const uint16_t HIGH_ADDR = 0xF000;
const uint8_t WIDTH = 64;
const uint8_t HEIGHT = 64;
const uint8_t PIXEL_WEIGHT = 8;
Memory& memory;
SDL_Event event;
SDL_Renderer* renderer;
SDL_Window* window;
void draw_pixel(int x, int y, uint8_t colour);
public:
Terminal(Memory& memory);
Terminal(const Terminal&) = delete;

View File

@ -1,6 +1,7 @@
#include <iostream>
#include <memory>
#include <vector>
#include <fstream>
#include "machine/machine.h"
#include "machine/terminal.h"
#include "machine/memory.h"
@ -11,20 +12,17 @@ using namespace std;
using namespace emu_6502;
int main() {
ifstream in("/home/tony/CLionProjects/6502-emulator/sample/a.o65", ios::binary);
if (in.fail())
throw runtime_error("Failed to read program file");
vector<uint8_t> code{0xa9, 53, 0xA5, 16};
vector<uint8_t> code(
(istreambuf_iterator<char>(in)),
(istreambuf_iterator<char>()));
auto machine = make_unique<Machine>();
machine->load(code, 0x600);
machine->execute();
// auto memory = make_shared<Memory>();
// auto term = make_unique<Terminal>(memory);
//
// term->refresh();
return 0;
}

View File

@ -1,7 +1,7 @@
#include "test-utils.h"
unique_ptr<Machine> create_machine(vector<uint8_t> code) {
auto machine = make_unique<Machine>();
auto machine = make_unique<Machine>(false);
machine->load(code, CODE_LOAD_ADDR);
return machine;