1
0
mirror of https://github.com/rkujawa/rk65c02.git synced 2025-03-04 20:31:10 +00:00
rk65c02/src/bus.c

77 lines
1.1 KiB
C
Raw Normal View History

#include <stdio.h>
2017-01-16 19:35:28 +01:00
#include <stdint.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
2017-01-16 19:35:28 +01:00
#include <assert.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
2017-01-16 19:35:28 +01:00
#define RK65C02_BUS_SIZE 64*1024
struct bus_tag {
uint8_t *space;
};
typedef struct bus_tag bus_t;
uint8_t
bus_read_1(bus_t *t, uint16_t addr)
{
return t->space[addr];
}
void
bus_write_1(bus_t *t, uint16_t addr, uint8_t val)
{
2017-01-24 22:18:21 +01:00
/* printf("bus WRITE @ %x value %x\n", addr, val); */
2017-01-16 19:35:28 +01:00
t->space[addr] = val;
}
bus_t
bus_init()
{
bus_t t;
t.space = (uint8_t *) malloc(RK65C02_BUS_SIZE);
assert(t.space != NULL);
memset(t.space, 0, RK65C02_BUS_SIZE);
return t;
}
bool
bus_load_file(bus_t *t, uint16_t addr, const char *filename)
{
int fd;
uint8_t data;
fd = open(filename, O_RDONLY);
if (fd == -1) {
perror("Problem while trying to open file");
return false;
}
while ((read(fd, &data, 1)) > 0) {
t->space[addr++] = data; // XXX: overflow addr
}
close(fd);
return true;
}
2017-01-16 19:35:28 +01:00
void
bus_finish(bus_t *t)
{
2017-01-16 21:34:56 +01:00
assert(t != NULL);
assert(t->space != NULL);
2017-01-16 19:35:28 +01:00
free(t->space);
}