Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include "xcpu.h"
- int main(int argc, char* argv[]){
- int i = 0;
- int k = 0;
- int status;
- int cycles;
- char code;
- FILE* file;
- struct xcpu_context c;
- c.memory = malloc(sizeof(unsigned char)*0xFFFF); //Memory allocation of 0s, 65536
- c.pc=0; //Program Counter = 0
- c.state=0x0000; //State initiated to 0
- for(i=0;i<=15;i++){ //All registers set to 0
- c.regs[i]=0x0000;
- }
- if(argc!=3){
- printf("Format is xsim cycles filename\n");
- exit(0);
- }
- if((file=fopen(argv[2], "r")) == NULL){
- printf("Unable to open File %s\n", argv[2]);
- exit(0);
- }
- cycles=atoi(argv[1]); //Transforms from int to string type
- if(cycles<0){
- printf("Cycles must be 0 and higher\n");
- exit(0);
- }
- while(fread(&code, sizeof(char), 1, file)){
- c.memory[k]=code;
- k++;
- if(k==0xFFFF){
- printf("ERROR: Out of memory\n");
- exit(0);
- }
- }
- if(cycles == 0){ //Cycle execution
- while(status != 0){
- status = xcpu_execute(&c);
- if(status == 0){
- printf("MESSAGE: CPU has halted.\n");
- break;
- }
- if(((c.state&0x0002)>>1) == 0x0001){
- xcpu_print(&c);
- }
- }
- }
- else{
- for(k = 0; k <= cycles; k++){
- status = xcpu_execute(&c);
- if(status == 0){
- printf("MESSAGE: CPU has halted.\n");
- break;
- }
- if((c.state&0x0002)>>1 == 1){
- xcpu_print(&c);
- }
- }
- if(status==1) printf("WARNING: CPU has timed-out.\n");
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement