First/simdata.c

114 lines
3.0 KiB
C

/* simdata.c
This file is part of the "First" CPU simulator project.
Copyright (c) 2024 Efthymios Kritikos
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#include <stdlib.h>
#include "simdata.h"
#include <stdio.h>
#include "gui.h" // for cleaning the cpu output
struct simdata_t *init_simdata(){
struct simdata_t * ret=malloc(sizeof(struct simdata_t));
if(!ret){
ret->current_clock=0;
return 0;
}
ret->RAM=malloc(16777216); //Maximum addressable memory
if(!ret->RAM){
printf("failed to allocate 16MiB of memory for the system ram");
free(ret);
return 0;
}
ret->registers=NULL;
ret->cpu_state=CPU_RUNNING;
ret->cpu_gui_hints=malloc(sizeof(struct cpu_gui_hints_t));
ret->cpu_gui_hints->decoding_list=NULL;
ret->cpu_gui_hints->fetching_list=NULL;
ret->cpu_gui_hints->executing_list=NULL;
ret->terminal_output=malloc(0);
ret->terminal_output_size=0;
ret->cpu_structure=CPU_STRUCTURE_SIMPLE_PIPELINED;
return ret;
}
uint8_t output_counter=0;
int terminal_output(uint32_t c,struct simdata_t *simdata){
simdata->terminal_output=realloc(simdata->terminal_output,simdata->terminal_output_size+4);
if(!simdata->terminal_output)
return 1;
simdata->terminal_output[simdata->terminal_output_size/4]=c;
simdata->terminal_output_size=simdata->terminal_output_size+4;
output_counter++;
if((output_counter%100)==0)
clean_cpu_output(simdata);
return 0;
}
void free_instr_list(struct instr_list_t **addr_of_tofree){
struct instr_list_t *tofree=*addr_of_tofree;
while(tofree){
struct instr_list_t *temp=tofree;
tofree=tofree->next;
free(temp);
}
*addr_of_tofree=0;
}
int add_to_instr_list(struct instr_list_t **toadd,uint32_t address){
while(*toadd){
toadd=&((*toadd)->next);
}
*toadd=malloc(sizeof(struct instr_list_t));
(*toadd)->address=address;
(*toadd)->next=NULL;
return 0;
}
void free_simdata(struct simdata_t *p){
free(p->RAM);
free_instr_list(&p->cpu_gui_hints->decoding_list);
free_instr_list(&p->cpu_gui_hints->fetching_list);
free_instr_list(&p->cpu_gui_hints->executing_list);
free(p->cpu_gui_hints);
free(p->terminal_output);
free(p);
}
#if defined ( __STDC_IEC_559__ )
union U32FLOAT_RETURN{
float f;
uint32_t u;
};
float uint32_to_ieee754_float(uint32_t in){
union U32FLOAT_RETURN ret;
ret.u=in;
return ret.f;
}
uint32_t ieee754_float_to_uint32(float in){
union U32FLOAT_RETURN ret;
ret.f=in;
return ret.u;
}
#else
#error Your libc/compiler/architecture doesnt support IEEE 457/IEC 559 Please write a software alternative here
#endif