Skip to content

Commit

Permalink
finish hooking everything up, needs testing
Browse files Browse the repository at this point in the history
  • Loading branch information
celerizer committed Oct 17, 2024
1 parent 6424560 commit ce999db
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 34 deletions.
54 changes: 53 additions & 1 deletion device.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ static const h8_system_preset_t h8_systems[] =
{ DEVICES_END }
}
},
{ NULL, H8_SYSTEM_INVALID, { 0 }, { NULL } }
{ NULL, H8_SYSTEM_INVALID, { 0 }, { { DEVICES_END } } }
};

h8_bool h8_device_init(h8_device_t *device, const h8_device_id type)
Expand Down Expand Up @@ -195,6 +195,11 @@ h8_bool h8_system_init(h8_system_t *system, const h8_system_id id)
break;
else
{
h8_system_pin_in_t *pins_in;
h8_system_pin_out_t *pins_out;
unsigned pin_count;
unsigned k;

/* Create the device if it does not already exist */
if (device->type == H8_DEVICE_INVALID)
{
Expand All @@ -204,6 +209,53 @@ h8_bool h8_system_init(h8_system_t *system, const h8_system_id id)

/* Setup IO functions */
device->port = hookup->port;

/* Figure out which port we're on */
switch (device->port)
{
case H8_HOOKUP_PORT_1:
pins_in = system->pdr1_in;
pins_out = system->pdr1_out;
pin_count = 3;
break;
case H8_HOOKUP_PORT_3:
pins_in = system->pdr3_in;
pins_out = system->pdr3_out;
pin_count = 3;
break;
case H8_HOOKUP_PORT_8:
pins_in = system->pdr8_in;
pins_out = system->pdr8_out;
pin_count = 3;
break;
case H8_HOOKUP_PORT_9:
pins_in = system->pdr9_in;
pins_out = system->pdr9_out;
pin_count = 4;
break;
case H8_HOOKUP_PORT_B:
pins_in = system->pdrb_in;
pins_out = system->pdrb_out;
pin_count = 6;
break;
default:
continue;
}

/* Hookup any used pins */
for (k = 0; k < pin_count; k++)
{
if (hookup->pdr_ins[k])
{
pins_in[k].device = device;
pins_in[k].func = hookup->pdr_ins[k];
}
if (hookup->pdr_outs[k])
{
pins_out[k].device = device;
pins_out[k].func = hookup->pdr_outs[k];
}
}
}
}

Expand Down
4 changes: 3 additions & 1 deletion devices/eeprom.c
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,9 @@ void h8_eeprom_write(h8_device_t *device, h8_byte_t *dst, const h8_byte_t value)
{
h8_eeprom_t *eeprom = (h8_eeprom_t*)device->device;

if (eeprom->position == 0)
if (!eeprom->selected)
return;
else if (eeprom->position == 0)
{
eeprom->command = value.u;

Expand Down
106 changes: 74 additions & 32 deletions emu.c
Original file line number Diff line number Diff line change
Expand Up @@ -245,34 +245,52 @@ H8_SHL_OP(l, h8_long_t, 0x00000001, r, >>=)
#define H8_IO_DUMMY_IN H8_UNUSED(system); H8_UNUSED(byte);
#define H8_IO_DUMMY_OUT H8_UNUSED(system); H8_UNUSED(byte); H8_UNUSED(value);

H8_OUT(pdr1o)
H8_IN(pdr1i)
{
unsigned i;

for (i = 0; i < system->device_count; i++)
for (i = 0; i < 3; i++)
{
if (system->devices[i].port == H8_HOOKUP_PORT_1 &&
!(system->devices[i].select & value.u))
if (system->pdr1_in->device && system->pdr1_in->func)
{
system->ssu_device = &system->devices[i];
printf("Using %s as port 1 device\n", system->devices[i].name);
*byte = value;
return;
byte->u &= ~(1 << i);
byte->u |= (system->pdr1_in->func(system->pdr1_in->device) & 0x01) << i;
}
}
system->ssu_device = NULL;
printf("Couldn't find port 1 device for %u\n", value.u);
}

H8_OUT(pdr1o)
{
unsigned i;

for (i = 0; i < 3; i++)
if (system->pdr1_out[i].device && system->pdr1_out[i].func)
system->pdr1_out[i].func(system->pdr1_out[i].device, (value.u >> i) & 1);

*byte = value;
}

H8_IN(pdr3i)
{
unsigned i;

for (i = 0; i < 3; i++)
{
if (system->pdr3_in[i].device && system[i].pdr3_in->func)
{
byte->u &= ~(1 << i);
byte->u |= (system->pdr3_in[i].func(system->pdr3_in[i].device) & 0x01) << i;
}
}
}

H8_OUT(pdr3o)
{
unsigned i;

for (i = 0; i < system->device_count; i++)
if (system->devices[i].port == H8_HOOKUP_PORT_3 &&
!(system->devices[i].select & value.u))
system->ssu_device = &system->devices[i];
for (i = 0; i < 3; i++)
if (system->pdr3_out[i].device && system[i].pdr3_out->func)
system->pdr3_out[i].func(system->pdr3_out[i].device, (value.u >> i) & 1);

*byte = value;
}
Expand All @@ -281,32 +299,53 @@ H8_OUT(pdr3o)
* 8.3.1 Port Data Register 8 (PDR8)
*/

H8_IN(pdr8i)
{
unsigned i;

for (i = 0; i < 3; i++)
{
if (system->pdr8_in[i].device && system->pdr8_in[i].func)
{
byte->u &= ~(1 << (i + 2));
byte->u |= (system->pdr8_in[i].func(system->pdr8_in[i].device) & 0x01) << (i + 2);
}
}
}

H8_OUT(pdr8o)
{
unsigned i;

for (i = 0; i < system->device_count; i++)
if (system->devices[i].port == H8_HOOKUP_PORT_8)
system->devices[i].write(&system->devices[i], byte, value);
for (i = 0; i < 3; i++)
if (system->pdr8_out[i].device && system[i].pdr8_out->func)
system->pdr8_out[i].func(system->pdr8_out[i].device, (value.u >> (i + 2)) & 1);

*byte = value;
}

H8_OUT(pdr9o)
H8_IN(pdr9i)
{
unsigned i;

for (i = 0; i < system->device_count; i++)
for (i = 0; i < 4; i++)
{
if (system->devices[i].port == H8_HOOKUP_PORT_9 &&
!(system->devices[i].select & value.u))
if (system->pdr9_in[i].device && system[i].pdr9_in->func)
{
system->ssu_device = &system->devices[i];
*byte = value;
return;
byte->u &= ~(1 << i);
byte->u |= (system->pdr9_in[i].func(system->pdr9_in[i].device) & 0x01) << i;
}
}
system->ssu_device = NULL;
}

H8_OUT(pdr9o)
{
unsigned i;

for (i = 0; i < 4; i++)
if (system->pdr9_out[i].device && system[i].pdr9_out->func)
system->pdr9_out[i].func(system->pdr9_out[i].device, (value.u >> i) & 1);

*byte = value;
}

Expand All @@ -318,11 +357,14 @@ H8_IN(pdrbi)
{
unsigned i;

for (i = 0; i < system->device_count; i++)
if (system->devices[i].port == H8_HOOKUP_PORT_B)
system->devices[i].read(&system->devices[i], byte);

byte->u &= B00111111;
for (i = 0; i < 6; i++)
{
if (system->pdrb_in[i].device && system[i].pdrb_in->func)
{
byte->u &= ~(1 << i);
byte->u |= (system[i].pdrb_in->func(system->pdrb_in[i].device) & 0x01) << i;
}
}
}

H8_OUT(pdrbo)
Expand Down Expand Up @@ -479,8 +521,8 @@ static H8_IN_T reg_ins[0x160] =
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
/* 0xFFD0 */
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, pdrbi, NULL,
NULL, NULL, NULL, NULL, pdr1i, NULL, pdr3i, NULL,
pdr8i, NULL, NULL, NULL, pdr9i, NULL, pdrbi, NULL,
/* 0xFFE0 */
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
Expand Down
27 changes: 27 additions & 0 deletions system.h
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,18 @@ typedef void (*H8_OUT_T)(struct h8_system_t*, h8_byte_t*, const h8_byte_t);
#define H8_IN(a) void a(h8_system_t* system, h8_byte_t* byte)
#define H8_OUT(a) void a(h8_system_t* system, h8_byte_t* byte, h8_byte_t value)

typedef struct
{
H8D_OP_PDR_IN_T *func;
h8_device_t *device;
} h8_system_pin_in_t;

typedef struct
{
H8D_OP_PDR_OUT_T *func;
h8_device_t *device;
} h8_system_pin_out_t;

typedef struct h8_system_t
{
h8_cpu_t cpu;
Expand All @@ -266,6 +278,21 @@ typedef struct h8_system_t
/** The number of devices initialized within `devices` */
unsigned device_count;

h8_system_pin_in_t pdr1_in[3];
h8_system_pin_out_t pdr1_out[3];

h8_system_pin_in_t pdr3_in[3];
h8_system_pin_out_t pdr3_out[3];

h8_system_pin_in_t pdr8_in[3];
h8_system_pin_out_t pdr8_out[3];

h8_system_pin_in_t pdr9_in[4];
h8_system_pin_out_t pdr9_out[4];

h8_system_pin_in_t pdrb_in[6];
h8_system_pin_out_t pdrb_out[6];

/** Whether or not SLEEP mode is currently active */
h8_bool sleep;

Expand Down

0 comments on commit ce999db

Please sign in to comment.