79 lines
1.8 KiB
C
79 lines
1.8 KiB
C
#include "mem.h"
|
|
#include <stdio.h>
|
|
|
|
unsigned char memory[4096];
|
|
|
|
extern unsigned char DEFAULT_FONT[];
|
|
|
|
void MemInit(){
|
|
MemSet(0,0,4096);
|
|
MemStore(DEFAULT_FONT,16*5,ADDR_FONT);
|
|
}
|
|
|
|
void MemSet(int addr, unsigned char value, int size){
|
|
for(int i=0;i<size;i++)
|
|
memory[addr+i]=value;
|
|
}
|
|
|
|
void MemStore(unsigned char *data, int size, int addr){
|
|
for(int i=0;i<size;i++){
|
|
if(addr+i>4096){
|
|
printf("Illegal memory access (store): addr=%d\n",addr+i);
|
|
return;
|
|
}
|
|
memory[addr+i]=data[i];
|
|
}
|
|
}
|
|
|
|
void MemLoad(unsigned char *data, int size, int addr){
|
|
int location=addr;
|
|
for(int i=0;i<size;i++){
|
|
if(addr+i>4096){
|
|
printf("Illegal memory access (load): addr=%d\n",addr+i);
|
|
return;
|
|
}
|
|
data[i]=memory[addr+i];
|
|
}
|
|
}
|
|
|
|
void MemLoadROM(char *path){
|
|
char byte;
|
|
FILE *ptr;
|
|
ptr=fopen(path,"rb");
|
|
int location=ADDR_ROM;
|
|
while(fread(&byte,1,1,ptr)==1){
|
|
if(location >= 4096)
|
|
break;
|
|
MemSet(location,byte,1);
|
|
location++;
|
|
}
|
|
fclose(ptr);
|
|
}
|
|
|
|
void MemDump(){
|
|
MemDumpRange(0,4096);
|
|
}
|
|
|
|
void MemDumpRange(int from, int size){
|
|
for(int addr=from;addr<from+size;addr+=2){
|
|
printf("0x%03x: %02x %02x\n",addr,memory[addr], memory[addr+1]);
|
|
}
|
|
}
|
|
|
|
unsigned char DEFAULT_FONT[]={
|
|
0xF0, 0x90, 0x90, 0x90, 0xF0, // 0
|
|
0x20, 0x60, 0x20, 0x20, 0x70, // 1
|
|
0xF0, 0x10, 0xF0, 0x80, 0xF0, // 2
|
|
0xF0, 0x10, 0xF0, 0x10, 0xF0, // 3
|
|
0x90, 0x90, 0xF0, 0x10, 0x10, // 4
|
|
0xF0, 0x80, 0xF0, 0x10, 0xF0, // 5
|
|
0xF0, 0x80, 0xF0, 0x90, 0xF0, // 6
|
|
0xF0, 0x10, 0x20, 0x40, 0x40, // 7
|
|
0xF0, 0x90, 0xF0, 0x90, 0xF0, // 8
|
|
0xF0, 0x90, 0xF0, 0x10, 0xF0, // 9
|
|
0xF0, 0x90, 0xF0, 0x90, 0x90, // A
|
|
0xE0, 0x90, 0xE0, 0x90, 0xE0, // B
|
|
0xF0, 0x80, 0x80, 0x80, 0xF0, // C
|
|
0xE0, 0x90, 0x90, 0x90, 0xE0, // D
|
|
0xF0, 0x80, 0xF0, 0x80, 0xF0, // E
|
|
0xF0, 0x80, 0xF0, 0x80, 0x80}; // F
|