COMMON/emulator/main.c

126 lines
3.1 KiB
C
Raw Normal View History

2018-08-07 02:14:36 +00:00
#include <stdint.h>
#include <stdio.h>
#include "emulator.h"
/* Register I6 maintains common status */
2018-10-30 07:38:42 +00:00
#define _R0 0xb0
#define _R8 0xd0
#define _I0 0xd8
#define _I6 0xf0
#define _I7 0xf4
2018-10-30 07:38:42 +00:00
#define _I8 0xf8
2018-08-07 02:14:36 +00:00
/* (dd cc bb aa) aa: index for register stack RS / ccbb: program counter PC / dd: flags F UONPZLGE */
#define _RSI _I6 /* register stack index */
2018-08-07 02:14:36 +00:00
#define _PCL _RSI + 1 /* program counter low */
#define _PCH _PCL + 1 /* program counter high */
#define _F _PCH + 1 /* flags */
#define _PC _PCL /* program counter */
2019-05-02 21:04:37 +00:00
/* register I7 maintains locations of allocated */
#define _ARLL _I7 /* allocated low and high bytes */
#define _ARLH _ARLL + 1
#define _ARUL _ARLH + 1 /* allocated upper limit */
#define _ARUH _ARUL + 1
#define _AR _ARLL /* allocated memory address */
/* section modifiers */
#define _SM_FXD 0x01
#define _SM_RLC 0x02
#define _SM_CD 0x04
#define _SM_DT 0x08
/* section identifiers */
#define _RLC_CD _SM_RLC + _SM_CD /* relocatable code */
#define _RLC_DT _SM_RLC + _SM_DT /* relocatable data */
2018-08-07 02:14:36 +00:00
uint8_t memory[65536];
uint8_t read6502(uint16_t address) {
return memory[address];
}
void write6502(uint16_t address, uint8_t value) {
memory[address] = value;
}
void hook() {
int i, j;
printf("\n%04x %u %u\n", pc, instructions, clockticks6502);
for (i = _R0; i < _R8; i += 4) {
printf("R%d: ", (i - _R0) / 4);
for (j = 0; j < 4; ++j)
printf("%02x ", memory[i + j]);
if (((i - _R0) / 4) % 4 == 3)
printf("\n");
}
for (i = _I0; i < _I8; i += 4) {
printf("I%d: ", (i - _I0) / 4);
for (j = 0; j < 4; ++j)
printf("%02x ", memory[i + j]);
if (((i - _I0) / 4) % 4 == 3)
printf("\n");
2018-08-07 02:14:36 +00:00
}
}
int main() {
uint8_t header[5];
/* where to start relocatables */
uint16_t index = 0x0600;
2018-08-07 02:14:36 +00:00
while (fread(header, sizeof(header), 1, stdin))
{
uint8_t type = header[0];
uint16_t entity = header[1] + (header[2] << 8);
uint16_t length = header[3] + (header[4] << 8);
2018-08-07 02:14:36 +00:00
printf("\n%x %04x %u\n", type, entity, length);
switch (type) {
case _SM_FXD: /* fixed code or data */
/* entity is the address, length is the length of the code or data */
fread(memory + entity, length, 1, stdin);
break;
case _RLC_CD: /* relocatable code */
/* entity is the starting offset, length is the length of code */
if (fread(memory + index, length, 1, stdin)) {
/* offset the starting address */
entity += index;
/* save the starting address */
2019-05-02 21:04:37 +00:00
memory[_PCL] = entity & 0xff;
memory[_PCH] = entity >> 8;
/* advance to the end of the section */
index += length;
}
break;
case _RLC_DT: /* relocatable data */
/* entity is the length of zeroed data, length is the length of preset data */
if (fread(memory + index, length, 1, stdin)) {
/* save the start of the data */
2019-05-02 21:04:37 +00:00
memory[_ARLL] = index & 0xff;
memory[_ARLH] = index >> 8;
/* advance to the end of the section */
index += entity + length;
2019-05-02 21:04:37 +00:00
/* save the end of the data */
memory[_ARUL] = index & 0xff;
memory[_ARUH] = index >> 8;
}
break;
2018-08-07 02:14:36 +00:00
}
}
hookexternal(hook);
reset6502();
do
step6502();
while (memory[pc]);
return 0;
}