This repository has been archived on 2024-08-24. You can view files and clone it, but cannot push or open issues or pull requests.
PlutoISA/cpu.hpp
2024-08-23 10:37:56 +02:00

89 lines
2.8 KiB
C++

/*
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 <https://www.gnu.org/licenses/>.
*/
#pragma once
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstdint>
#include <chrono>
#include <thread>
#include "tpu.hpp"
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 ) );
void cpuInstructions ( uint8_t *rom ) {
cout << static_cast<int> ( rom[*pointer] ) << endl;
cout << static_cast<int> ( *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 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;
case 0x07: // HIN
cin >> registers[rom[*pointer + 1]];
*pointer += 2;
break;
default:
cout << "Invalid Instruction, quitting." << endl;
exit ( 1 );
}
}
void cpuEmulation ( uint8_t *rom ) {
const int targetFPS = 20;
const int frameDuration = 1000 / targetFPS;
*pointer = 0;
while ( true ) {
auto start = chrono::high_resolution_clock::now();
cpuInstructions ( rom );
textProcessing ( registers[7] );
auto end = chrono::high_resolution_clock::now();
chrono::duration<double, milli> elapsed = end - start;
if ( elapsed.count() < frameDuration ) {
this_thread::sleep_for ( chrono::milliseconds ( frameDuration ) - elapsed );
}
}
}