instructions

This commit is contained in:
Rose Apollo 2024-04-19 12:11:28 +00:00
parent 764f2406dd
commit e6e01700f7
7 changed files with 146 additions and 34 deletions

5
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,5 @@
{
"files.associations": {
"ram.h": "c"
}
}

View File

@ -1,32 +1,32 @@
# r16 # r16
| number | instruction | argument 1 | argument 2 | explanation | | number | instruction | argument 1 | argument 2 | fullname | explanation |
|--------|-------------|------------|------------|---------------------------------------------------| |-------:|:-----------:|------------|------------|----------------------|---------------------------------------------------|
| 0x00 | HLT | | | halts the CPU | | 0x00 | HLT | | | Halt | halts the CPU |
| 0x01 | ADD | value 1 | value 2 | ACU = value 1 + value 2 | | 0x01 | ADD | value 1 | value 2 | Add | ACU = value 1 + value 2 |
| 0x02 | SUB | value 1 | value 2 | ACU = value 1 - value 2 | | 0x02 | SUB | value 1 | value 2 | Subtract | ACU = value 1 - value 2 |
| 0x03 | MUL | value 1 | value 2 | ACU = value 1 * value 2 | | 0x03 | MUL | value 1 | value 2 | Multiply | ACU = value 1 * value 2 |
| 0x04 | DIV | value 1 | value 2 | ACU = value 1 / value 2 | | 0x04 | DIV | value 1 | value 2 | Divide | ACU = value 1 / value 2 |
| 0x05 | LBS | value | amount | ACU = value << amount | | 0x05 | LBS | value | amount | Left Bit-Shift | ACU = value << amount |
| 0x06 | RBS | value | amount | ACU = value << amount | | 0x06 | RBS | value | amount | Right Bit-Shift | ACU = value << amount |
| 0x07 | BAN | value 1 | value 2 | ACU = value 1 & value 2 | | 0x07 | BAN | value 1 | value 2 | Bitwise And | ACU = value 1 & value 2 |
| 0x08 | BOR | value 1 | value 2 | ACU = value 1 \ | | 0x08 | BOR | value 1 | value 2 | Bitwise Or | ACU = value 1 \ |
| 0x09 | BXO | value 1 | value 2 | ACU = value 1 ^ value 2 | | 0x09 | BXO | value 1 | value 2 | Bitwise Exclusive Or | ACU = value 1 ^ value 2 |
| 0x0A | BNO | value | | ACU = ~ value | | 0x0A | BNO | value | | Bitwise Not | ACU = ~ value |
| 0x0B | PUS | value | | pushes value onto the stack | | 0x0B | PUS | value | | Push | pushes value onto the stack |
| 0x0C | POP | | | pops a value from the stack and stores it in ACU | | 0x0C | POP | | | Pop | pops a value from the stack and stores it in ACU |
| 0x0D | JMP | address | | jumps to address | | 0x0D | JMP | address | | Jump | jumps to address |
| 0x0E | JEQ | address | value | jumps to address, if value is equal to ACU | | 0x0E | JEQ | address | value | Jump If Equal | jumps to address, if value is equal to ACU |
| 0x0F | JNZ | address | value | jumps to address, if value is not zero | | 0x0F | JNZ | address | value | Jump If Not Zero | jumps to address, if value is not zero |
| 0x10 | CAL | address | | pushes PC + 1 to the stack, then jumps to address | | 0x10 | CAL | address | | Call | pushes PC + 1 to the stack, then jumps to address |
| 0x11 | RET | | | pops value from stack and jumps to it | | 0x11 | RET | | | Return | pops value from stack and jumps to it |
| 0x12 | REG | int type | address | registers int type to address | | 0x12 | REG | int type | address | Register Interrupt | registers int type to address |
| 0x13 | INT | int type | | interrupts as int type | | 0x13 | INT | int type | | Interrupt | interrupts as int type |
| 0x14 | INP | | | inputs a character into ACU | | 0x14 | INP | | | Input | inputs a character into ACU |
| 0x15 | OUT | value | | outputs value | | 0x15 | OUT | value | | Output | outputs value |
| 0x16 | DIN | location | | reads value from disk at location to ACU | | 0x16 | DIN | location | | Disk In | reads value from disk at location to ACU |
| 0x17 | DOT | location | value | writes value to disk at location | | 0x17 | DOT | location | value | Disk Out | writes value to disk at location |
| 0x18 | MOV | value 1 | value 2 | move value 2 to value 1 | | 0x18 | MOV | value 1 | value 2 | Move | move value 2 to value 1 |
| Number | Register | Usage | | Number | Register | Usage |
|-------:|---------:|-----------------| |-------:|---------:|-----------------|

View File

@ -1,2 +1,2 @@
mkdir ./bin mkdir ./bin
clang -o ./bin/r16e ./emu/main.c ./emu/cpu/ram.c ./emu/cpu/register.c ./emu/cpu/stack.c ./lib/carg-parse/carg-parse.c clang -o ./bin/r16e ./emu/main.c ./emu/cpu/instruction.c ./emu/cpu/ram.c ./emu/cpu/register.c ./emu/cpu/stack.c ./lib/carg-parse/carg-parse.c

14
emu/cpu.c Normal file
View File

@ -0,0 +1,14 @@
#include "cpu.h"
#include <stdbool.h>
void Run(r16_int entry)
{
r16_int instruction, arg1, arg2;
SetRegister(PC, entry);
while (true)
{
}
}

9
emu/cpu.h Normal file
View File

@ -0,0 +1,9 @@
#ifndef EMU_CPU_H
#define EMU_CPU_H
#include "cpu_const.h"
#include "cpu/ram.h"
#include "cpu/register.h"
#include "cpu/instruction.h"
#endif

View File

@ -1,41 +1,122 @@
#include "./instruction.h" #include "./instruction.h"
#include <stdio.h>
#include <stdlib.h>
r16_int GetArgVal(ArgumentInfo argInfo, r16_int arg) r16_int GetArgVal(ArgumentInfo argInfo, r16_int arg)
{ {
switch (argInfo)
{
case value:
return arg;
case valueInRegister:
return GetRegister((CpuRegisters)arg.u);
case valueInRam:
return GetValue(arg);
case pointerInRegister:
return GetValue(GetRegister((CpuRegisters)arg.u));
}
}
void GetAllArgVal(ArgumentInfo arg1Info, ArgumentInfo arg2Info,
r16_int arg1, r16_int arg2,
r16_int *arg1Out, r16_int *arg2Out)
{
*arg1Out = GetArgVal(arg1Info, arg1);
*arg2Out = GetArgVal(arg2Info, arg2);
} }
void ExecuteInstruction(CpuInstructions instruction, ArgumentInfo arg1Info, void ExecuteInstruction(CpuInstructions instruction, ArgumentInfo arg1Info,
ArgumentInfo arg2Info, r16_int arg1, r16_int arg2) ArgumentInfo arg2Info, r16_int arg1, r16_int arg2)
{ {
r16_int actArg1, actArg2;
switch (instruction) switch (instruction)
{ {
case HLT: case HLT:
printf("program halted\n"); printf("\nprogram HALTED!\n");
exit(0); exit(0);
case ADD: case ADD:
GetAllArgVal(arg1Info, arg2Info, arg1, arg2, &actArg1, &actArg2);
SetRegister(ACU, (r16_int){ actArg1.s + actArg2.s });
break;
case SUB: case SUB:
GetAllArgVal(arg1Info, arg2Info, arg1, arg2, &actArg1, &actArg2);
SetRegister(ACU, (r16_int){ actArg1.s - actArg2.s });
break;
case MUL: case MUL:
GetAllArgVal(arg1Info, arg2Info, arg1, arg2, &actArg1, &actArg2);
SetRegister(ACU, (r16_int){ actArg1.s * actArg2.s });
break;
case DIV: case DIV:
GetAllArgVal(arg1Info, arg2Info, arg1, arg2, &actArg1, &actArg2);
SetRegister(ACU, (r16_int){ actArg1.s / actArg2.s });
break;
case LBS: case LBS:
GetAllArgVal(arg1Info, arg2Info, arg1, arg2, &actArg1, &actArg2);
SetRegister(ACU, (r16_int){ actArg1.s << actArg2.s });
break;
case RBS: case RBS:
GetAllArgVal(arg1Info, arg2Info, arg1, arg2, &actArg1, &actArg2);
SetRegister(ACU, (r16_int){ actArg1.s >> actArg2.s });
break;
case BAN: case BAN:
GetAllArgVal(arg1Info, arg2Info, arg1, arg2, &actArg1, &actArg2);
SetRegister(ACU, (r16_int){ actArg1.u & actArg2.u });
break;
case BOR: case BOR:
GetAllArgVal(arg1Info, arg2Info, arg1, arg2, &actArg1, &actArg2);
SetRegister(ACU, (r16_int){ actArg1.u | actArg2.u });
break;
case BXO: case BXO:
GetAllArgVal(arg1Info, arg2Info, arg1, arg2, &actArg1, &actArg2);
SetRegister(ACU, (r16_int){ actArg1.u ^ actArg2.u });
break;
case BNO: case BNO:
SetRegister(ACU, (r16_int){ ~ GetArgVal(arg1Info, arg1).u });
break;
case PUS: case PUS:
StackPush(GetArgVal(arg1Info, arg1));
break;
case POP: case POP:
SetRegister(ACU, StackPop());
break;
case JMP: case JMP:
SetRegister(PC, GetArgVal(arg1Info, arg1));
break;
case JEQ: case JEQ:
GetAllArgVal(arg1Info, arg2Info, arg1, arg2, &actArg1, &actArg2);
if (actArg2.u == GetRegister(ACU).u)
SetRegister(PC, actArg1);
break;
case JNZ: case JNZ:
GetAllArgVal(arg1Info, arg2Info, arg1, arg2, &actArg1, &actArg2);
if (actArg2.s != 0)
SetRegister(PC, actArg1);
break;
case CAL: case CAL:
StackPush(GetRegister(PC));
SetRegister(PC, GetArgVal(arg1Info, arg1));
break;
case RET: case RET:
case REG: SetRegister(PC, StackPop());
case INT: break;
case REG: // TODO
case INT: // TODO
fprintf(stderr, "INTS NOT IMPLEMENTED YET\n");
exit(-1);
case INP: case INP:
SetRegister(ACU, (r16_int){ getchar() });
break;
case OUT: case OUT:
case DIN: putchar((char)(GetArgVal(arg1Info, arg1).u));
case DOT: break;
case DIN: // TODO
case DOT: // TODO
fprintf(stderr, "DISK NOT IMPLEMENTED YET\n");
exit(-1);
case MOV: case MOV:
GetAllArgVal(arg1Info, arg2Info, arg1, arg2, &actArg1, &actArg2);
SetValue(actArg1, actArg2);
break;
} }
} }

View File

@ -6,4 +6,7 @@
#include "./register.h" #include "./register.h"
#include "./stack.h" #include "./stack.h"
void ExecuteInstruction(CpuInstructions instruction, ArgumentInfo arg1Info,
ArgumentInfo arg2Info, r16_int arg1, r16_int arg2);
#endif #endif