Start of graphics support
This commit is contained in:
parent
af0099caa6
commit
737b50e211
|
@ -23,11 +23,16 @@ add_subdirectory(${CMAKE_CURRENT_BINARY_DIR}/googletest-src
|
||||||
${CMAKE_CURRENT_BINARY_DIR}/googletest-build
|
${CMAKE_CURRENT_BINARY_DIR}/googletest-build
|
||||||
EXCLUDE_FROM_ALL)
|
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")
|
file(GLOB_RECURSE test_src "test/**.h" "test/**.cpp" "src/**.h" "src/**.cpp")
|
||||||
list(FILTER test_src EXCLUDE REGEX ".*/6502-emulator/src/main.cpp$")
|
list(FILTER test_src EXCLUDE REGEX ".*/6502-emulator/src/main.cpp$")
|
||||||
add_executable(6502_emulator_test ${test_src})
|
add_executable(6502_emulator_test ${test_src})
|
||||||
target_link_libraries(6502_emulator_test gtest_main)
|
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)
|
add_test(NAME run_tests COMMAND 6502_emulator_test)
|
||||||
|
|
||||||
file(GLOB_RECURSE emulator_src "src/**.h" "src/**.cpp")
|
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})
|
|
@ -0,0 +1,9 @@
|
||||||
|
lda #$0
|
||||||
|
|
||||||
|
loop
|
||||||
|
sta $e000, x
|
||||||
|
inx
|
||||||
|
adc #$1
|
||||||
|
cpx #$ff
|
||||||
|
bne loop
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
#include "machine.h"
|
#include "machine.h"
|
||||||
|
#include "terminal.h"
|
||||||
#include "../opcode/opcode-handler-directory.h"
|
#include "../opcode/opcode-handler-directory.h"
|
||||||
|
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
@ -6,18 +7,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;
|
||||||
|
|
||||||
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<OpcodeHandlerDirectory> opcode_handler_dir;
|
unique_ptr<OpcodeHandlerDirectory> opcode_handler_dir;
|
||||||
public:
|
public:
|
||||||
MachineImpl() {
|
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());
|
||||||
|
|
||||||
|
if (init_terminal) {
|
||||||
|
terminal = make_unique<Terminal>(*memory);
|
||||||
|
terminal_initialised = true;
|
||||||
|
}
|
||||||
|
|
||||||
opcode_handler_dir = make_unique<OpcodeHandlerDirectory>();
|
opcode_handler_dir = make_unique<OpcodeHandlerDirectory>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -67,11 +76,14 @@ namespace emu_6502 {
|
||||||
while (!is_eop()) {
|
while (!is_eop()) {
|
||||||
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)
|
||||||
|
terminal->refresh();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
Machine::Machine() : pimpl(make_unique<MachineImpl>()) {
|
Machine::Machine(bool init_terminal) : pimpl(make_unique<MachineImpl>(init_terminal)) {
|
||||||
}
|
}
|
||||||
|
|
||||||
Machine::~Machine() = default;
|
Machine::~Machine() = default;
|
||||||
|
|
|
@ -17,7 +17,7 @@ namespace emu_6502 {
|
||||||
private:
|
private:
|
||||||
unique_ptr<MachineImpl> pimpl;
|
unique_ptr<MachineImpl> pimpl;
|
||||||
public:
|
public:
|
||||||
Machine();
|
Machine(bool init_terminal = true);
|
||||||
~Machine();
|
~Machine();
|
||||||
Machine(const Machine&) = delete;
|
Machine(const Machine&) = delete;
|
||||||
Machine& operator=(const Machine&) = delete;
|
Machine& operator=(const Machine&) = delete;
|
||||||
|
|
|
@ -5,33 +5,55 @@
|
||||||
|
|
||||||
namespace emu_6502 {
|
namespace emu_6502 {
|
||||||
Terminal::Terminal(Memory& memory) : memory(memory) {
|
Terminal::Terminal(Memory& memory) : memory(memory) {
|
||||||
// for (auto i = 0; i < 100; i++)
|
int width = WIDTH * PIXEL_WEIGHT;
|
||||||
// memory->set_at(Terminal::LOW_ADDR + i, 0x20 + i);
|
int height = HEIGHT * PIXEL_WEIGHT;
|
||||||
//
|
|
||||||
// if ((mainwin = initscr()) == NULL) {
|
SDL_Init(SDL_INIT_VIDEO);
|
||||||
// fprintf(stderr, "Error initialising ncurses.\n");
|
SDL_CreateWindowAndRenderer(width, height, 0, &window, &renderer);
|
||||||
// exit(EXIT_FAILURE);
|
SDL_RenderClear(renderer);
|
||||||
// }
|
|
||||||
//
|
for (auto i = LOW_ADDR; i <= HIGH_ADDR; i++) {
|
||||||
// start_color();
|
memory.set_at(i, 0);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Terminal::~Terminal() {
|
Terminal::~Terminal() {
|
||||||
// delwin(mainwin);
|
SDL_DestroyRenderer(renderer);
|
||||||
// endwin();
|
SDL_DestroyWindow(window);
|
||||||
|
SDL_Quit();
|
||||||
cout << "shutting down" << endl;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Terminal::refresh() {
|
void Terminal::refresh() {
|
||||||
// for (auto n = 0; n <= 100; n++) {
|
int x, y = 0;
|
||||||
// auto line = (n / TERM_COLS);
|
for (auto i = LOW_ADDR; i <= HIGH_ADDR; i++) {
|
||||||
// auto col = n % TERM_COLS;
|
x = (i - LOW_ADDR) % WIDTH;
|
||||||
//
|
y = (i - LOW_ADDR) / HEIGHT;
|
||||||
// mvaddch(line, col, memory->get_at(Terminal::LOW_ADDR + n));
|
|
||||||
// n++;
|
auto colour = memory.get_at(i);
|
||||||
// }
|
if (colour) {
|
||||||
//
|
draw_pixel(x, y, colour);
|
||||||
// wrefresh(mainwin);
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,8 +1,8 @@
|
||||||
#ifndef INC_6502_EMULATOR_TERMINAL_H
|
#ifndef INC_6502_EMULATOR_TERMINAL_H
|
||||||
#define INC_6502_EMULATOR_TERMINAL_H
|
#define INC_6502_EMULATOR_TERMINAL_H
|
||||||
|
|
||||||
#include <ncurses.h>
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <SDL2/SDL.h>
|
||||||
#include "memory.h"
|
#include "memory.h"
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
@ -10,15 +10,20 @@ using namespace std;
|
||||||
namespace emu_6502 {
|
namespace emu_6502 {
|
||||||
class Terminal {
|
class Terminal {
|
||||||
private:
|
private:
|
||||||
const uint16_t LOW_ADDR = 0xFB00;
|
const uint16_t LOW_ADDR = 0xE000;
|
||||||
const uint16_t HIGH_ADDR = 0xFF00;
|
const uint16_t HIGH_ADDR = 0xF000;
|
||||||
const uint8_t TERM_LINES = 25;
|
const uint8_t WIDTH = 64;
|
||||||
const uint8_t TERM_COLS = 40;
|
const uint8_t HEIGHT = 64;
|
||||||
|
const uint8_t PIXEL_WEIGHT = 8;
|
||||||
//WINDOW * mainwin;
|
|
||||||
|
|
||||||
Memory& memory;
|
Memory& memory;
|
||||||
|
|
||||||
|
SDL_Event event;
|
||||||
|
SDL_Renderer* renderer;
|
||||||
|
SDL_Window* window;
|
||||||
|
|
||||||
|
void draw_pixel(int x, int y, uint8_t colour);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Terminal(Memory& memory);
|
Terminal(Memory& memory);
|
||||||
Terminal(const Terminal&) = delete;
|
Terminal(const Terminal&) = delete;
|
||||||
|
|
16
src/main.cpp
16
src/main.cpp
|
@ -1,6 +1,7 @@
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <fstream>
|
||||||
#include "machine/machine.h"
|
#include "machine/machine.h"
|
||||||
#include "machine/terminal.h"
|
#include "machine/terminal.h"
|
||||||
#include "machine/memory.h"
|
#include "machine/memory.h"
|
||||||
|
@ -11,20 +12,17 @@ using namespace std;
|
||||||
using namespace emu_6502;
|
using namespace emu_6502;
|
||||||
|
|
||||||
int main() {
|
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>();
|
auto machine = make_unique<Machine>();
|
||||||
machine->load(code, 0x600);
|
machine->load(code, 0x600);
|
||||||
|
|
||||||
machine->execute();
|
machine->execute();
|
||||||
|
|
||||||
// auto memory = make_shared<Memory>();
|
|
||||||
// auto term = make_unique<Terminal>(memory);
|
|
||||||
//
|
|
||||||
// term->refresh();
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
|
@ -1,7 +1,7 @@
|
||||||
#include "test-utils.h"
|
#include "test-utils.h"
|
||||||
|
|
||||||
unique_ptr<Machine> create_machine(vector<uint8_t> code) {
|
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);
|
machine->load(code, CODE_LOAD_ADDR);
|
||||||
|
|
||||||
return machine;
|
return machine;
|
||||||
|
|
Loading…
Reference in New Issue