r16/emu/cpu.c

39 lines
973 B
C
Raw Permalink Normal View History

2024-04-19 12:11:28 +00:00
#include "cpu.h"
#include <stdbool.h>
2024-04-19 12:39:59 +00:00
void GetInstructionAndArgs(r16_int *instruct, r16_int *a1, r16_int *a2,
ArgumentInfo *a1i, ArgumentInfo *a2i)
{
r16_int tmpInstruct, tmpPC;
tmpPC = GetRegister(PC);
tmpInstruct = GetValue(tmpPC);
tmpPC.u++;
*a1 = GetValue(tmpPC);
tmpPC.u++;
*a2 = GetValue(tmpPC);
tmpPC.u++;
SetRegister(PC, tmpPC);
*instruct = (r16_int){ tmpInstruct.u & INSTRUCTION_BIT_MASK };
*a1i = (ArgumentInfo)((tmpInstruct.u & INSTRUCTION_ARG_1_INFO_MASK) >> 12);
*a2i = (ArgumentInfo)((tmpInstruct.u & INSTRUCTION_ARG_2_INFO_MASK) >> 14);
}
2024-04-19 12:11:28 +00:00
void Run(r16_int entry)
{
r16_int instruction, arg1, arg2;
2024-04-19 12:39:59 +00:00
ArgumentInfo arg1Info, arg2Info;
2024-04-19 12:11:28 +00:00
SetRegister(PC, entry);
while (true)
{
2024-04-19 12:39:59 +00:00
GetInstructionAndArgs(&instruction, &arg1, &arg2, &arg1Info, &arg2Info);
2024-04-19 12:11:28 +00:00
2024-04-19 12:39:59 +00:00
ExecuteInstruction((CpuInstructions)instruction.u, arg1Info, arg2Info, arg1, arg2);
2024-04-19 12:11:28 +00:00
}
}