forked from PlaneteCasio/Casio_asm
154 lines
3.1 KiB
C
Executable File
154 lines
3.1 KiB
C
Executable File
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
|
|
#include "stack.h"
|
|
#include "asm.h"
|
|
#include "debug.h"
|
|
|
|
void printState(asm_t *state);
|
|
void pushOperation(char* rom, int *pos, opcode_t *opcode);
|
|
void writeProgram(asm_t *state);
|
|
|
|
int main(int argc, char** argv) {
|
|
asm_t state;
|
|
char ram[65536];
|
|
char rom[65536];
|
|
state.ramSize=65536;
|
|
state.romSize=65536;
|
|
state.ram=ram;
|
|
state.rom=rom;
|
|
initAsm(&state);
|
|
|
|
writeProgram(&state);
|
|
run(&state, 1000);
|
|
printState(&state);
|
|
}
|
|
|
|
void printState(asm_t *state) {
|
|
printf("--asm state--\n");
|
|
printf("PC: 0x%x; romSize: %d\n", state->registers[0].i, state->romSize);
|
|
printf("IntHandler: 0x%x\n", state->registers[1].i);
|
|
printf("Stack size: %d\n", state->stack.top);
|
|
for(int i=0; i<state->stack.top; i++) {
|
|
printf("\tStack element %d: %d or %lf\n", i, state->stack.elements[i].i, state->stack.elements[i].d);
|
|
}
|
|
printf("Registers:\n");
|
|
for(int i=2; i<256; i++) {
|
|
if(state->registers[i].i==0) continue;
|
|
printf("\tRegister %d: %d or %lf\n", i, state->registers[i].i, state->registers[i].d);
|
|
}
|
|
printf("--end of asm state--\n");
|
|
}
|
|
|
|
void pushOperation(char* rom, int *pos, opcode_t *opcode) {
|
|
debug("Adding opcode %d with %d arguments at offset 0x%x\n", opcode->op, opcode->nArgs, *pos);
|
|
rom[*pos]=opcode->byte0;
|
|
(*pos)++;
|
|
if(opcode->nArgs>0) {
|
|
rom[*pos]=opcode->byte1;
|
|
(*pos)++;
|
|
if(opcode->nArgs>1) {
|
|
rom[*pos]=opcode->byte2;
|
|
(*pos)++;
|
|
}
|
|
}
|
|
}
|
|
|
|
void writeProgram(asm_t *state) {
|
|
int pos=0;
|
|
opcode_t opcode;
|
|
|
|
//push 0i
|
|
opcode.nArgs=3;
|
|
opcode.op=OP_push;
|
|
opcode.immediate=0;
|
|
pushOperation(state->rom, &pos, &opcode);
|
|
|
|
//store #2
|
|
opcode.nArgs=1;
|
|
opcode.op=OP_store;
|
|
opcode.arg0_1=2;
|
|
pushOperation(state->rom, &pos, &opcode);
|
|
|
|
//push 1i
|
|
opcode.nArgs=3;
|
|
opcode.op=OP_push;
|
|
opcode.immediate=1;
|
|
pushOperation(state->rom, &pos, &opcode);
|
|
|
|
//store #3
|
|
opcode.nArgs=1;
|
|
opcode.op=OP_store;
|
|
opcode.arg0_1=3;
|
|
pushOperation(state->rom, &pos, &opcode);
|
|
|
|
//.loop:
|
|
int loop=pos;
|
|
|
|
//add_i #2 #3
|
|
opcode.nArgs=2;
|
|
opcode.op=OP_add_i;
|
|
opcode.arg0_2=2;
|
|
opcode.arg1_2=3;
|
|
pushOperation(state->rom, &pos, &opcode);
|
|
|
|
//store #2
|
|
opcode.nArgs=1;
|
|
opcode.op=OP_store;
|
|
opcode.arg0_1=2;
|
|
pushOperation(state->rom, &pos, &opcode);
|
|
|
|
//add_i #2 #3
|
|
opcode.nArgs=2;
|
|
opcode.op=OP_add_i;
|
|
opcode.arg0_2=2;
|
|
opcode.arg1_2=3;
|
|
pushOperation(state->rom, &pos, &opcode);
|
|
|
|
//store #3
|
|
opcode.nArgs=1;
|
|
opcode.op=OP_store;
|
|
opcode.arg0_1=3;
|
|
pushOperation(state->rom, &pos, &opcode);
|
|
|
|
//high 0x1000i
|
|
opcode.nArgs=3;
|
|
opcode.op=OP_high;
|
|
opcode.immediate=0x1000;
|
|
pushOperation(state->rom, &pos, &opcode);
|
|
|
|
//push #3
|
|
opcode.nArgs=1;
|
|
opcode.op=OP_push;
|
|
opcode.arg0_1=3;
|
|
pushOperation(state->rom, &pos, &opcode);
|
|
|
|
//jle_i loop
|
|
opcode.nArgs=3;
|
|
opcode.op=OP_jle_i;
|
|
opcode.immediate=loop;
|
|
pushOperation(state->rom, &pos, &opcode);
|
|
|
|
//i2d #2
|
|
opcode.nArgs=1;
|
|
opcode.op=OP_i2d;
|
|
opcode.arg0_1=2;
|
|
pushOperation(state->rom, &pos, &opcode);
|
|
|
|
//i2d #3
|
|
opcode.nArgs=1;
|
|
opcode.op=OP_i2d;
|
|
opcode.arg0_1=3;
|
|
pushOperation(state->rom, &pos, &opcode);
|
|
|
|
//div_d
|
|
opcode.nArgs=0;
|
|
opcode.op=OP_div_d;
|
|
pushOperation(state->rom, &pos, &opcode);
|
|
|
|
//halt
|
|
opcode.nArgs=0;
|
|
opcode.op=OP_halt;
|
|
pushOperation(state->rom, &pos, &opcode);
|
|
}
|