mirror of
https://github.com/tuz358/cpu-emulator.git
synced 2025-12-18 21:14:35 +03:00
first commit
This commit is contained in:
32
emulator.cpp
Normal file
32
emulator.cpp
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
#include "include/emulator.h"
|
||||||
|
|
||||||
|
Instructions instructions;
|
||||||
|
Memory memory;
|
||||||
|
|
||||||
|
void Emulator::init(size_t memorysize, FILE *bin){
|
||||||
|
memory.init(memorysize);
|
||||||
|
memory.load_binary(bin);
|
||||||
|
instructions.init(0, (int)memorysize/2);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Emulator::free(){
|
||||||
|
memory.free_memory();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Emulator::exec(uint8_t opcode){
|
||||||
|
instructions.execute_opcode(opcode);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Emulator::dump_registers(){
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t Emulator::read_next_opcode(){
|
||||||
|
uint8_t opcode = memory.read_uint8(instructions.eip);
|
||||||
|
instructions.eip++;
|
||||||
|
|
||||||
|
return opcode;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t Emulator::get_eip(){
|
||||||
|
return instructions.eip;
|
||||||
|
}
|
||||||
17
include/emulator.h
Normal file
17
include/emulator.h
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
#define KB 1024
|
||||||
|
#define MB 1024*KB
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include "instructions.h"
|
||||||
|
#include "memory.h"
|
||||||
|
|
||||||
|
class Emulator{
|
||||||
|
public:
|
||||||
|
void init(size_t memorysize, FILE *bin);
|
||||||
|
void free();
|
||||||
|
void exec(uint8_t opcode);
|
||||||
|
void dump_registers();
|
||||||
|
uint8_t read_next_opcode();
|
||||||
|
uint32_t get_eip();
|
||||||
|
};
|
||||||
22
include/instructions.h
Normal file
22
include/instructions.h
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
class Instructions{
|
||||||
|
private:
|
||||||
|
void init_instructions();
|
||||||
|
public:
|
||||||
|
uint32_t eax, ecx, edx, ebx; // General purpose
|
||||||
|
uint32_t esp, ebp, esi, edi; // registers
|
||||||
|
uint32_t eflags; // EFLAGS register
|
||||||
|
uint32_t eip; // Instruction pointer
|
||||||
|
|
||||||
|
void (Instructions::*instructions[256])(void);
|
||||||
|
|
||||||
|
void init(uint32_t eip, uint32_t esp);
|
||||||
|
void execute_opcode(uint8_t opcode);
|
||||||
|
|
||||||
|
void nop();
|
||||||
|
void hlt();
|
||||||
|
};
|
||||||
14
include/memory.h
Normal file
14
include/memory.h
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
class Memory {
|
||||||
|
private:
|
||||||
|
uint8_t *memory;
|
||||||
|
size_t memsize;
|
||||||
|
public:
|
||||||
|
uint8_t *init(size_t memorysize);
|
||||||
|
uint8_t read_uint8(uint32_t addr);
|
||||||
|
size_t get_memsize();
|
||||||
|
void load_binary(FILE *file);
|
||||||
|
void free_memory();
|
||||||
|
};
|
||||||
41
instructions.cpp
Normal file
41
instructions.cpp
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
#include "include/instructions.h"
|
||||||
|
|
||||||
|
void Instructions::init(uint32_t eip, uint32_t esp){
|
||||||
|
// initialize general purpose registers
|
||||||
|
this->eax = 0;
|
||||||
|
this->ecx = 0;
|
||||||
|
this->edx = 0;
|
||||||
|
this->ebx = 0;
|
||||||
|
this->esp = esp;
|
||||||
|
this->ebp = 0;
|
||||||
|
this->esi = 0;
|
||||||
|
this->edi = 0;
|
||||||
|
|
||||||
|
// initialize eflags register
|
||||||
|
this->eflags = 0;
|
||||||
|
|
||||||
|
// initialize instruction pointer
|
||||||
|
this->eip = eip;
|
||||||
|
|
||||||
|
this->init_instructions();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Instructions::init_instructions(){
|
||||||
|
memset(this->instructions, 0, sizeof(this->instructions));
|
||||||
|
|
||||||
|
this->instructions[0x90] = &Instructions::nop;
|
||||||
|
this->instructions[0xf4] = &Instructions::hlt;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Instructions::execute_opcode(uint8_t opcode){
|
||||||
|
(this->*instructions[opcode])();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Instructions::nop(){
|
||||||
|
printf("nop called.\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
void Instructions::hlt(){
|
||||||
|
printf("hlt called.\n");
|
||||||
|
this->eip = 0x00;
|
||||||
|
}
|
||||||
25
main.cpp
Normal file
25
main.cpp
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
#include "include/emulator.h"
|
||||||
|
|
||||||
|
const size_t MEMORY_SIZE = 1*KB;
|
||||||
|
|
||||||
|
int main(int argc, char *argv[]){
|
||||||
|
FILE *bin;
|
||||||
|
bin = fopen(argv[1], "rb");
|
||||||
|
|
||||||
|
Emulator emulator;
|
||||||
|
emulator.init(MEMORY_SIZE, bin);
|
||||||
|
fclose(bin);
|
||||||
|
|
||||||
|
while(emulator.get_eip() < MEMORY_SIZE) {
|
||||||
|
uint8_t opcode = emulator.read_next_opcode();
|
||||||
|
emulator.exec(opcode);
|
||||||
|
|
||||||
|
if (emulator.get_eip() == 0x00) break;
|
||||||
|
}
|
||||||
|
|
||||||
|
emulator.free();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
25
memory.cpp
Normal file
25
memory.cpp
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
#include "include/memory.h"
|
||||||
|
|
||||||
|
uint8_t *Memory::init(size_t memorysize){
|
||||||
|
this->memsize = memorysize;
|
||||||
|
this->memory = (uint8_t *)malloc(memsize);
|
||||||
|
|
||||||
|
return this->memory;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t Memory::read_uint8(uint32_t addr){
|
||||||
|
return this->memory[addr];
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t Memory::get_memsize(){
|
||||||
|
return this->memsize;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Memory::load_binary(FILE *file){
|
||||||
|
// fread(this->memory, size_t __size, size_t __nitems, file);
|
||||||
|
fread(this->memory, 1, 0x200, file);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Memory::free_memory(){
|
||||||
|
free(this->memory);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user