emulator/src/main.c

75 lines
2.6 KiB
C
Raw Normal View History

#include <stdio.h>
#include <inttypes.h>
#include <stdbool.h>
2017-08-02 20:10:36 +02:00
#include "toy.h"
#include "helper.h"
2017-08-02 20:10:36 +02:00
#include <assert.h>
2017-08-09 22:51:27 +02:00
#ifndef NDEBUG
2017-09-27 16:25:58 +02:00
#define DEBUG
2017-08-09 22:51:27 +02:00
#endif
2017-08-02 20:10:36 +02:00
int main(int argc, char *argv[])
{
uint16_t ram[RAM_SIZE];
2017-08-17 15:28:30 +02:00
uint16_t ir=0; //Instruction Register
uint8_t op_code; //CPU Operation Code
uint16_t pc = 0; //Program Counter
2017-10-04 16:23:55 +02:00
uint16_t data_addr=0; //Address of the 2nd operand (1. is ACCU)
2017-08-17 15:28:30 +02:00
int quit='n'; //Helper for program-flow exit (not part of CPU)
2017-10-04 16:23:55 +02:00
bool run = true; //CPU halt and reset.(Nb: make a coredump beforehand)
2017-08-02 20:10:36 +02:00
printf("\n+++++++++++++++++++++++++++++++++++++++++++++++\n+Boot: "CPU_TYPE
2017-10-04 23:11:13 +02:00
" with %lu x %lu Bit RAM.+\n+++++++++++++++++++++++++++++++++++++++++++++++\n\n"
,(unsigned long)(sizeof(ram)/sizeof(ram[0])),(unsigned long)sizeof(ram[0])*8);
2017-09-27 16:25:58 +02:00
if(initialise_ram(ram,argc,argv)==-1) return 1; /*load data from command line into RAM
(-1 in case of error,
2017-10-04 16:23:55 +02:00
else number of correctly read words)*/
2017-08-19 13:07:24 +02:00
while(run)
2017-08-10 23:08:50 +02:00
{
ir = ram[pc]; //get instruction from RAM
op_code = get_opcode(ir); //determine the instruction form
data_addr=get_data(ir); /*locate the 2nd operand
2017-10-04 16:23:55 +02:00
(undefined for OP_Codes 8 to 15)*/
//handle user output
printf("\ninstruction:\t");
2017-08-14 13:29:24 +02:00
fprintBits(sizeof(*ram), ram+pc,stdout);
printf("OP CODE: %"PRIu8"\tmeans: ",op_code);
print_instruction(op_code);
printf("address: %"PRIu16"\tcontent:%"
PRIu16"\n",data_addr,ram[data_addr]);
printf("RAM position:\t%"PRIu16"\n",pc);
printf("\ninstruction result:\n");
2017-08-17 15:28:30 +02:00
//CPU control flow
2017-10-04 16:23:55 +02:00
if(execute(op_code,data_addr,ram)) //EXECUTE instruction,jump on ALU
pc=get_data(ir);
2017-09-27 16:25:58 +02:00
else pc++;
2017-10-04 16:23:55 +02:00
if(pc>=RAM_SIZE) pc %= RAM_SIZE; //TOY_CPU can only address 12 bits
//handle user output
printf("ACCU: %d\n",get2compl(*ACCU));
printf("PROGRAM COUNTER: %" PRIu16
"\n----------------------------------------",pc);
printf("\n|(n)ext step or (q)uit or (c)oredump ? ");
//handle program flow
while((quit = getchar()) != '\n')
{
switch(quit)
{
case EOF: break;
case 'c': makeHexDump(true,ram); break;
case 'q': run = false;
}
}
printf("----------------------------------------\n");
2017-08-10 23:08:50 +02:00
}
2017-08-02 20:10:36 +02:00
return 0;
}