diff --git a/.gitignore b/.gitignore index 662f6d5..38dcb00 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ .vscode/ bin/ +*.kdev4 +.kdev4/ diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..3d81273 --- /dev/null +++ b/Makefile @@ -0,0 +1,14 @@ +# Compiler +CXX = g++ +# Source file +SRC = main.cpp +# Executable name +EXEC = pluto_isa_emu + +# Default target +all: + mkdir -p bin/ + $(CXX) $(SRC) -o bin/$(EXEC) + +clean: + rm bin/$(EXEC) diff --git a/README.md b/README.md index f62e5d0..43008e4 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,3 @@ # PlutoISA +This is an emulator for the custom made PlutoISA. diff --git a/cpu.hpp b/cpu.hpp index 9508c24..5c3a4ff 100644 --- a/cpu.hpp +++ b/cpu.hpp @@ -1,42 +1,71 @@ +/* +PlutoISA +Copyright (C) 2024 Patrick_Pluto + +This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + +This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + +You should have received a copy of the GNU General Public License along with this program. If not, see . +*/ + +#pragma once + #include +#include +#include #include -namespace plutoisa -{ +namespace plutoisa { using namespace std; - uint8_t *registers = (uint8_t *)malloc(8 * sizeof(uint8_t)); - uint8_t *memory = (uint8_t *)malloc(128 * sizeof(uint8_t)); - uint8_t *pointer = (uint8_t *)malloc(sizeof(uint8_t)); + uint8_t *registers = ( uint8_t * ) malloc ( 8 * sizeof ( uint8_t ) ); + uint8_t *memory = ( uint8_t * ) malloc ( 128 * sizeof ( uint8_t ) ); + uint8_t *pointer = ( uint8_t * ) malloc ( sizeof ( uint8_t ) ); - void cpuInstructions(uint8_t *rom) - { - switch (rom[*pointer]) - { - case 0x00: // LOAD R0 M0 + void cpuInstructions ( uint8_t *rom ) { + cout << static_cast(rom[*pointer]) << endl; + cout << static_cast(*pointer) << endl; + switch ( rom[*pointer] ) { + case 0x00: // GET + registers[rom[*pointer + 1]] = rom[*pointer + 2]; + *pointer += 3; + break; + case 0x01: // LOAD registers[rom[*pointer + 1]] = memory[rom[*pointer + 2]]; *pointer += 3; break; - case 0x01: // STORE R0 M0 + case 0x02: // STORE memory[rom[*pointer + 2]] = registers[rom[*pointer + 1]]; *pointer += 3; break; + case 0x03: // ADD + registers[rom[*pointer + 1]] = registers[rom[*pointer + 2]] + registers[rom[*pointer + 3]]; + *pointer += 4; + break; + case 0x04: // SUB + registers[rom[*pointer + 1]] = registers[rom[*pointer + 2]] - registers[rom[*pointer + 3]]; + *pointer += 4; + break; + case 0x05: // JMP + *pointer = rom[*pointer + 1]; + break; + case 0x06: // JEQ + if ( rom[*pointer + 2] == registers[rom[*pointer + 3]] ) { + *pointer = rom[*pointer + 1]; + } + break; default: - cout << "Invalid Instruction, expect broken behavior" << endl; - exit(1); + cout << "Invalid Instruction, quitting." << endl; + exit ( 1 ); } } - void cpuEmulation(uint8_t *rom) - { + void cpuEmulation ( uint8_t *rom ) { *pointer = 0; - memory[0] = 0x12; - - while (true) - { - cout << static_cast(registers[0]) << endl; - cpuInstructions(rom); + while ( true ) { + cpuInstructions ( rom ); } } -} \ No newline at end of file +} diff --git a/main.cpp b/main.cpp index f93369e..6b52ab4 100644 --- a/main.cpp +++ b/main.cpp @@ -1,13 +1,27 @@ +/* +PlutoISA +Copyright (C) 2024 Patrick_Pluto + +This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + +This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + +You should have received a copy of the GNU General Public License along with this program. If not, see . +*/ + #include "cpu.hpp" using namespace plutoisa; -int main() -{ - uint8_t *rom = (uint8_t *)malloc(16 * sizeof(uint8_t)); - rom[0] = 0x00; - rom[1] = 0x00; - rom[2] = 0x00; - rom[3] = 0xFF; - cpuEmulation(rom); -} \ No newline at end of file +int main ( int argc, char *argv[] ) { + const char *filename = argv[1]; + const int rom_size = 16; + + ifstream file ( filename, ios::binary ); + + uint8_t *rom = ( uint8_t * ) malloc ( rom_size * sizeof ( uint8_t ) ); + + file.read ( reinterpret_cast ( rom ), rom_size ); + + cpuEmulation ( rom ); +}