1
0
mirror of https://github.com/mist64/perfect6502.git synced 2024-06-25 20:29:40 +00:00
perfect6502/perfect6502.c

979 lines
18 KiB
C
Raw Normal View History

2010-09-22 07:35:22 +00:00
//#define DEBUG
2010-09-22 15:25:34 +00:00
int verbose = 0;
2010-09-22 05:14:57 +00:00
2010-09-22 01:51:38 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef unsigned char uint8_t;
2010-09-22 05:14:57 +00:00
typedef unsigned short uint16_t;
2010-09-22 01:51:38 +00:00
typedef int BOOL;
2010-09-22 22:57:00 +00:00
typedef uint16_t nodenum_t;
2010-09-22 23:02:36 +00:00
typedef uint16_t transnum_t;
typedef uint16_t count_t;
typedef uint8_t state_t;
2010-09-22 22:57:00 +00:00
2010-09-22 01:51:38 +00:00
#define NO 0
#define YES 1
2010-09-22 18:00:45 +00:00
#define SWAPLIST(a,b) {list_t tmp = a; a = b; b = tmp; }
2010-09-22 01:51:38 +00:00
#include "segdefs.h"
#include "transdefs.h"
#include "nodenames.h"
#define ngnd vss
#define npwr vcc
uint8_t code[] = { 0xa9, 0x00, 0x20, 0x10, 0x00, 0x4c, 0x02, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0xe8, 0x88, 0xe6, 0x40, 0x38, 0x69, 0x02, 0x60 };
enum {
STATE_VCC,
2010-09-22 17:37:35 +00:00
STATE_PU,
2010-09-22 01:51:38 +00:00
STATE_FH,
2010-09-22 17:37:35 +00:00
STATE_GND,
STATE_FL,
2010-09-22 01:51:38 +00:00
STATE_PD,
2010-09-22 17:37:35 +00:00
STATE_UNDEFINED,
2010-09-22 01:51:38 +00:00
};
2010-09-22 17:37:35 +00:00
#define MAX_HIGH STATE_FH /* VCC, PU and FH are considered high */
2010-09-22 07:35:22 +00:00
#define NODES 1725
#define TRANSISTORS 3510
2010-09-22 01:51:38 +00:00
typedef struct {
BOOL pullup;
BOOL pulldown;
2010-09-22 23:02:36 +00:00
state_t state;
2010-09-22 22:57:00 +00:00
nodenum_t gates[NODES];
nodenum_t c1c2s[2*NODES];
2010-09-22 23:02:36 +00:00
count_t gatecount;
count_t c1c2count;
2010-09-22 01:51:38 +00:00
} node_t;
2010-09-22 07:35:22 +00:00
node_t nodes[NODES];
2010-09-22 01:51:38 +00:00
#define EMPTY -1
2010-09-22 05:14:57 +00:00
typedef struct {
2010-09-22 23:02:36 +00:00
transnum_t name;
2010-09-22 22:57:00 +00:00
nodenum_t gate;
nodenum_t c1;
nodenum_t c2;
2010-09-22 05:14:57 +00:00
} transistor_t;
2010-09-22 07:35:22 +00:00
transistor_t transistors[TRANSISTORS];
int transistors_on[TRANSISTORS/sizeof(int)];
void
set_transistors_on(transnum_t t, BOOL state)
{
if (state)
transistors_on[t>>5] |= 1 << (t & 31);
else
transistors_on[t>>5] &= ~(1 << (t & 31));
}
BOOL
get_transistors_on(transnum_t t)
{
return (transistors_on[t>>5] >> (t & 31)) & 1;
}
2010-09-22 01:51:38 +00:00
uint8_t memory[65536];
int cycle;
2010-09-22 10:05:49 +00:00
uint8_t A, X, Y, S, P;
uint16_t PC;
BOOL N, Z, C;
2010-09-22 01:51:38 +00:00
void
setupNodes()
{
2010-09-22 23:02:36 +00:00
count_t i;
2010-09-22 01:51:38 +00:00
for (i = 0; i < sizeof(segdefs)/sizeof(*segdefs); i++) {
nodes[i].pullup = segdefs[i];
nodes[i].state = STATE_FL;
nodes[i].gatecount = 0;
nodes[i].c1c2count = 0;
}
}
void
setupTransistors()
{
2010-09-22 23:02:36 +00:00
count_t i;
2010-09-22 01:51:38 +00:00
for (i = 0; i < sizeof(transdefs)/sizeof(*transdefs); i++) {
2010-09-22 22:57:00 +00:00
nodenum_t gate = transdefs[i].gate;
nodenum_t c1 = transdefs[i].c1;
nodenum_t c2 = transdefs[i].c2;
2010-09-22 01:51:38 +00:00
transistors[i].name = i;
transistors[i].gate = gate;
transistors[i].c1 = c1;
transistors[i].c2 = c2;
nodes[gate].gates[nodes[gate].gatecount++] = i;
nodes[c1].c1c2s[nodes[c1].c1c2count++] = i;
nodes[c2].c1c2s[nodes[c2].c1c2count++] = i;
}
}
2010-09-22 05:14:57 +00:00
#ifdef DEBUG
void
2010-09-22 23:02:36 +00:00
printarray(nodenum_t *array, count_t count)
2010-09-22 05:14:57 +00:00
{
2010-09-22 23:02:36 +00:00
count_t i;
2010-09-22 05:14:57 +00:00
for (i = 0; i < count; i++)
printf("%d ", array[i]);
printf("\n");
}
#endif
2010-09-22 22:57:00 +00:00
nodenum_t group[NODES];
2010-09-22 23:02:36 +00:00
count_t groupcount;
2010-09-22 22:51:14 +00:00
int groupbitmap[NODES/sizeof(int)];
2010-09-22 22:38:56 +00:00
2010-09-22 01:51:38 +00:00
BOOL
2010-09-22 22:57:00 +00:00
arrayContains(nodenum_t el)
2010-09-22 01:51:38 +00:00
{
2010-09-22 22:46:11 +00:00
#if 0
2010-09-22 23:02:36 +00:00
count_t i;
2010-09-22 22:38:56 +00:00
for (i = 0; i < groupcount; i++) {
if (group[i] == el) {
2010-09-22 01:51:38 +00:00
return YES;
2010-09-22 05:14:57 +00:00
}
2010-09-22 01:51:38 +00:00
}
return NO;
2010-09-22 22:46:11 +00:00
#else
2010-09-22 22:51:14 +00:00
return (groupbitmap[el>>5] >> (el & 31)) & 1;
2010-09-22 22:46:11 +00:00
#endif
2010-09-22 01:51:38 +00:00
}
2010-09-22 18:00:45 +00:00
typedef struct {
2010-09-22 22:57:00 +00:00
nodenum_t *list;
2010-09-22 23:02:36 +00:00
count_t count;
2010-09-22 22:49:11 +00:00
char *bitmap;
2010-09-22 18:00:45 +00:00
} list_t;
list_t recalc;
void
clearRecalc()
{
2010-09-22 22:46:11 +00:00
bzero(recalc.bitmap, sizeof(*recalc.bitmap)*NODES);
2010-09-22 18:00:45 +00:00
recalc.count = 0;
}
BOOL
2010-09-22 22:57:00 +00:00
recalcListContains(nodenum_t el)
2010-09-22 18:00:45 +00:00
{
return recalc.bitmap[el];
}
2010-09-22 22:57:00 +00:00
void addNodeToGroup(nodenum_t i);
2010-09-22 01:51:38 +00:00
void
2010-09-22 23:02:36 +00:00
addNodeTransistor(nodenum_t node, transnum_t t)
2010-09-22 01:51:38 +00:00
{
2010-09-22 05:14:57 +00:00
#ifdef DEBUG
printf("%s n=%d, t=%d, group=", __func__, node, t);
2010-09-22 18:06:15 +00:00
printarray(group, groupcount);
2010-09-22 05:14:57 +00:00
#endif
if (!get_transistors_on(t))
2010-09-22 01:51:38 +00:00
return;
2010-09-22 22:57:00 +00:00
nodenum_t other;
2010-09-22 01:51:38 +00:00
if (transistors[t].c1 == node)
other = transistors[t].c2;
if (transistors[t].c2 == node)
other = transistors[t].c1;
2010-09-22 18:06:15 +00:00
addNodeToGroup(other);
2010-09-22 01:51:38 +00:00
}
void
2010-09-22 22:57:00 +00:00
addNodeToGroup(nodenum_t i)
2010-09-22 01:51:38 +00:00
{
2010-09-22 05:14:57 +00:00
#ifdef DEBUG
printf("%s %d, group=", __func__, i);
2010-09-22 18:06:15 +00:00
printarray(group, groupcount);
2010-09-22 05:14:57 +00:00
#endif
2010-09-22 22:38:56 +00:00
if (arrayContains(i))
2010-09-22 01:51:38 +00:00
return;
2010-09-22 18:06:15 +00:00
group[groupcount++] = i;
2010-09-22 22:51:14 +00:00
groupbitmap[i>>5] |= 1 << (i & 31);
2010-09-22 01:51:38 +00:00
if (i == ngnd)
return;
if (i == npwr)
return;
2010-09-22 23:02:36 +00:00
count_t t;
2010-09-22 01:51:38 +00:00
for (t = 0; t < nodes[i].c1c2count; t++)
2010-09-22 18:06:15 +00:00
addNodeTransistor(i, nodes[i].c1c2s[t]);
2010-09-22 01:51:38 +00:00
}
2010-09-22 23:02:36 +00:00
state_t
2010-09-22 18:06:15 +00:00
getNodeValue()
2010-09-22 01:51:38 +00:00
{
2010-09-22 05:14:57 +00:00
#ifdef DEBUG
printf("%s group=", __func__);
printarray(group, groupcount);
#endif
2010-09-22 22:38:56 +00:00
if (arrayContains(ngnd))
2010-09-22 01:51:38 +00:00
return STATE_GND;
2010-09-22 22:38:56 +00:00
if (arrayContains(npwr))
2010-09-22 01:51:38 +00:00
return STATE_VCC;
2010-09-22 23:02:36 +00:00
state_t flstate = STATE_UNDEFINED;
count_t i;
2010-09-22 01:51:38 +00:00
for (i = 0; i < groupcount; i++) {
2010-09-22 22:57:00 +00:00
nodenum_t nn = group[i];
2010-09-22 01:51:38 +00:00
node_t n = nodes[nn];
if (n.pullup)
return STATE_PU;
if (n.pulldown)
return STATE_PD;
if ((n.state == STATE_FL) && (flstate == STATE_UNDEFINED))
flstate = STATE_FL;
if (n.state== STATE_FH)
flstate = STATE_FH;
}
return flstate;
}
2010-09-22 05:14:57 +00:00
void
2010-09-22 22:57:00 +00:00
addRecalcNode(nodenum_t nn)
2010-09-22 05:14:57 +00:00
{
#ifdef DEBUG
2010-09-22 18:00:45 +00:00
printf("%s nn=%d recalc.list=", __func__, nn);
printarray(recalc.list, recalc.count);
2010-09-22 05:14:57 +00:00
#endif
2010-09-22 18:00:45 +00:00
if (nn == ngnd || nn == npwr)
2010-09-22 05:14:57 +00:00
return;
2010-09-22 18:00:45 +00:00
if (recalcListContains(nn))
2010-09-22 05:14:57 +00:00
return;
2010-09-22 18:06:15 +00:00
recalc.list[recalc.count++] = nn;
2010-09-22 18:00:45 +00:00
recalc.bitmap[nn] = 1;
2010-09-22 05:14:57 +00:00
}
void
2010-09-22 22:57:00 +00:00
floatnode(nodenum_t nn)
2010-09-22 05:14:57 +00:00
{
#ifdef DEBUG
printf("%s nn=%d\n", __func__, nn);
#endif
2010-09-22 17:37:35 +00:00
if (nn == ngnd || nn == npwr)
2010-09-22 05:14:57 +00:00
return;
2010-09-22 23:02:36 +00:00
state_t state = nodes[nn].state;
2010-09-22 17:37:35 +00:00
if (state == STATE_GND || state == STATE_PD)
2010-09-22 07:25:32 +00:00
nodes[nn].state = STATE_FL;
2010-09-22 17:37:35 +00:00
if (state == STATE_VCC || state == STATE_PU)
2010-09-22 07:25:32 +00:00
nodes[nn].state = STATE_FH;
2010-09-22 05:14:57 +00:00
#ifdef DEBUG
printf("%s %i to state %d\n", __func__, nn, n.state);
#endif
}
BOOL
2010-09-22 22:57:00 +00:00
isNodeHigh(nodenum_t nn)
2010-09-22 05:14:57 +00:00
{
#ifdef DEBUG
2010-09-22 07:25:32 +00:00
printf("%s nn=%d state=%d\n", __func__, nn, nodes[nn].state);
2010-09-22 17:37:35 +00:00
printf("%s nn=%d res=%d\n", __func__, nn, nodes[nn].state <= MAX_HIGH);
2010-09-22 05:14:57 +00:00
#endif
2010-09-22 17:37:35 +00:00
return nodes[nn].state <= MAX_HIGH;
2010-09-22 05:14:57 +00:00
}
void
2010-09-22 23:02:36 +00:00
recalcTransistor(transnum_t tn)
2010-09-22 05:14:57 +00:00
{
#ifdef DEBUG
2010-09-22 18:00:45 +00:00
printf("%s tn=%d, recalc.list=", __func__, tn);
printarray(recalc.list, recalc.count);
2010-09-22 05:14:57 +00:00
#endif
2010-09-22 07:25:32 +00:00
transistor_t *t = &transistors[tn];
2010-09-22 17:00:22 +00:00
BOOL on = isNodeHigh(t->gate);
if (on == get_transistors_on(tn))
2010-09-22 17:00:22 +00:00
return;
set_transistors_on(tn, on);
2010-09-22 17:00:22 +00:00
if (!on) {
floatnode(t->c1);
floatnode(t->c2);
}
2010-09-22 17:40:32 +00:00
addRecalcNode(t->c1);
addRecalcNode(t->c2);
2010-09-22 05:14:57 +00:00
}
2010-09-22 01:51:38 +00:00
void
2010-09-22 22:57:00 +00:00
recalcNode(nodenum_t node)
2010-09-22 01:51:38 +00:00
{
2010-09-22 05:14:57 +00:00
#ifdef DEBUG
2010-09-22 18:00:45 +00:00
printf("%s node=%d, recalc.list=", __func__, node);
printarray(recalc.list, recalc.count);
2010-09-22 05:14:57 +00:00
#endif
2010-09-22 18:06:15 +00:00
if (node == ngnd || node == npwr)
2010-09-22 01:51:38 +00:00
return;
2010-09-22 18:06:15 +00:00
groupcount = 0;
2010-09-22 22:46:11 +00:00
bzero(groupbitmap, sizeof(groupbitmap));
2010-09-22 18:06:15 +00:00
addNodeToGroup(node);
2010-09-22 01:51:38 +00:00
2010-09-22 23:02:36 +00:00
state_t newv = getNodeValue();
count_t i;
2010-09-22 05:14:57 +00:00
for (i = 0; i < groupcount; i++) {
node_t n = nodes[group[i]];
2010-09-22 07:25:32 +00:00
nodes[group[i]].state = newv;
2010-09-22 23:02:36 +00:00
count_t t;
2010-09-22 05:14:57 +00:00
for (t = 0; t < n.gatecount; t++)
2010-09-22 17:40:32 +00:00
recalcTransistor(n.gates[t]);
2010-09-22 05:14:57 +00:00
}
2010-09-22 01:51:38 +00:00
}
void
2010-09-22 23:02:36 +00:00
recalcNodeList(nodenum_t *list, count_t count)
2010-09-22 01:51:38 +00:00
{
2010-09-22 05:14:57 +00:00
#ifdef DEBUG
printf("%s list=", __func__);
printarray(list, count);
#endif
2010-09-22 22:57:00 +00:00
nodenum_t list1[NODES];
char bitmap1[NODES];
char bitmap2[NODES];
2010-09-22 18:00:45 +00:00
list_t current;
current.list = list;
current.count = count;
current.bitmap = bitmap2;
recalc.list = list1;
recalc.bitmap = bitmap1;
2010-09-22 23:02:36 +00:00
count_t i;
int j;
2010-09-22 01:51:38 +00:00
for (j = 0; j < 100; j++) { // loop limiter
2010-09-22 18:00:45 +00:00
clearRecalc();
if (!current.count)
2010-09-22 01:51:38 +00:00
return;
2010-09-22 05:14:57 +00:00
#ifdef DEBUG
2010-09-22 18:00:45 +00:00
printf("%s iteration=%d, current.list=", __func__, j);
printarray(current.list, current.count);
2010-09-22 05:14:57 +00:00
#endif
2010-09-22 18:00:45 +00:00
for (i = 0; i < current.count; i++)
recalcNode(current.list[i]);
SWAPLIST(current, recalc);
2010-09-22 01:51:38 +00:00
}
}
void
recalcAllNodes()
{
2010-09-22 05:14:57 +00:00
#ifdef DEBUG
printf("%s\n", __func__);
#endif
2010-09-22 07:35:22 +00:00
printf("%s count=%d\n", __func__, NODES);
2010-09-22 22:57:00 +00:00
nodenum_t list[NODES];
2010-09-22 23:02:36 +00:00
count_t i;
2010-09-22 07:35:22 +00:00
for (i = 0; i < NODES; i++)
2010-09-22 05:14:57 +00:00
list[i] = i;
2010-09-22 07:35:22 +00:00
recalcNodeList(list, NODES);
2010-09-22 01:51:38 +00:00
}
void
2010-09-22 22:57:00 +00:00
setLow(nodenum_t nn)
2010-09-22 01:51:38 +00:00
{
2010-09-22 05:14:57 +00:00
#ifdef DEBUG
printf("%s nn=%d\n", __func__, nn);
#endif
2010-09-22 01:51:38 +00:00
nodes[nn].pullup = NO;
nodes[nn].pulldown = YES;
2010-09-22 22:57:00 +00:00
nodenum_t list[NODES];
2010-09-22 07:35:22 +00:00
list[0] = nn;
recalcNodeList(list, 1);
2010-09-22 01:51:38 +00:00
}
void
2010-09-22 22:57:00 +00:00
setHigh(nodenum_t nn)
2010-09-22 01:51:38 +00:00
{
2010-09-22 05:14:57 +00:00
#ifdef DEBUG
printf("%s nn=%d\n", __func__, nn);
#endif
2010-09-22 01:51:38 +00:00
nodes[nn].pullup = YES;
nodes[nn].pulldown = NO;
2010-09-22 22:57:00 +00:00
nodenum_t list[NODES];
2010-09-22 07:35:22 +00:00
list[0] = nn;
recalcNodeList(list, 1);
2010-09-22 01:51:38 +00:00
}
2010-09-22 15:25:34 +00:00
void
2010-09-22 22:57:00 +00:00
setHighLow(nodenum_t nn, BOOL val)
2010-09-22 15:25:34 +00:00
{
if (val)
setHigh(nn);
else
setLow(nn);
}
2010-09-22 05:14:57 +00:00
void
writeDataBus(uint8_t x)
2010-09-22 01:51:38 +00:00
{
2010-09-22 22:57:00 +00:00
nodenum_t recalcs[NODES];
2010-09-22 23:02:36 +00:00
count_t recalcscount = 0;
2010-09-22 05:14:57 +00:00
int i;
for (i = 0; i < 8; i++) {
2010-09-22 22:57:00 +00:00
nodenum_t nn;
2010-09-22 05:14:57 +00:00
switch (i) {
case 0: nn = db0; break;
case 1: nn = db1; break;
case 2: nn = db2; break;
case 3: nn = db3; break;
case 4: nn = db4; break;
case 5: nn = db5; break;
case 6: nn = db6; break;
case 7: nn = db7; break;
}
if ((x & 1) == 0) {
2010-09-22 07:25:32 +00:00
nodes[nn].pulldown = YES;
nodes[nn].pullup = NO;
2010-09-22 05:14:57 +00:00
} else {
2010-09-22 07:25:32 +00:00
nodes[nn].pulldown = NO;
nodes[nn].pullup = YES;
2010-09-22 05:14:57 +00:00
}
recalcs[recalcscount++] = nn;
x >>= 1;
}
recalcNodeList(recalcs, recalcscount);
}
uint8_t mRead(uint16_t a)
{
2010-09-22 15:25:34 +00:00
if (verbose)
printf("PEEK($%04X) = $%02X\n", a, memory[a]);
2010-09-22 05:14:57 +00:00
return memory[a];
}
uint16_t
readAddressBus()
{
return (isNodeHigh(ab0) << 0) |
(isNodeHigh(ab1) << 1) |
(isNodeHigh(ab2) << 2) |
(isNodeHigh(ab3) << 3) |
(isNodeHigh(ab4) << 4) |
(isNodeHigh(ab5) << 5) |
(isNodeHigh(ab6) << 6) |
(isNodeHigh(ab7) << 7) |
(isNodeHigh(ab8) << 8) |
(isNodeHigh(ab9) << 9) |
(isNodeHigh(ab10) << 10) |
(isNodeHigh(ab11) << 11) |
(isNodeHigh(ab12) << 12) |
(isNodeHigh(ab13) << 13) |
(isNodeHigh(ab14) << 14) |
(isNodeHigh(ab15) << 15);
2010-09-22 01:51:38 +00:00
}
void
handleBusRead()
{
2010-09-22 05:14:57 +00:00
if (isNodeHigh(rw))
writeDataBus(mRead(readAddressBus()));
}
uint8_t
readDataBus()
{
return (isNodeHigh(db0) << 0) |
(isNodeHigh(db1) << 1) |
(isNodeHigh(db2) << 2) |
(isNodeHigh(db3) << 3) |
(isNodeHigh(db4) << 4) |
(isNodeHigh(db5) << 5) |
(isNodeHigh(db6) << 6) |
(isNodeHigh(db7) << 7);
}
void
mWrite(uint16_t a, uint8_t d)
{
2010-09-22 15:25:34 +00:00
if (verbose)
printf("POKE $%04X, $%02X\n", a, d);
2010-09-22 05:14:57 +00:00
memory[a] = d;
2010-09-22 01:51:38 +00:00
}
void
handleBusWrite()
{
2010-09-22 05:14:57 +00:00
if (!isNodeHigh(rw)) {
uint16_t a = readAddressBus();
uint8_t d = readDataBus();
mWrite(a,d);
}
}
2010-09-22 15:25:34 +00:00
void
setA(uint8_t val)
{
setHighLow(a0, (val >> 0) & 1);
setHighLow(a1, (val >> 1) & 1);
setHighLow(a2, (val >> 2) & 1);
setHighLow(a3, (val >> 3) & 1);
setHighLow(a4, (val >> 4) & 1);
setHighLow(a5, (val >> 5) & 1);
setHighLow(a6, (val >> 6) & 1);
setHighLow(a7, (val >> 7) & 1);
}
void
setX(uint8_t val)
{
setHighLow(x0, (val >> 0) & 1);
setHighLow(x1, (val >> 1) & 1);
setHighLow(x2, (val >> 2) & 1);
setHighLow(x3, (val >> 3) & 1);
setHighLow(x4, (val >> 4) & 1);
setHighLow(x5, (val >> 5) & 1);
setHighLow(x6, (val >> 6) & 1);
setHighLow(x7, (val >> 7) & 1);
}
void
setY(uint8_t val)
{
setHighLow(y0, (val >> 0) & 1);
setHighLow(y1, (val >> 1) & 1);
setHighLow(y2, (val >> 2) & 1);
setHighLow(y3, (val >> 3) & 1);
setHighLow(y4, (val >> 4) & 1);
setHighLow(y5, (val >> 5) & 1);
setHighLow(y6, (val >> 6) & 1);
setHighLow(y7, (val >> 7) & 1);
}
void
setSP(uint8_t val)
{
setHighLow(s0, (val >> 0) & 1);
setHighLow(s1, (val >> 1) & 1);
setHighLow(s2, (val >> 2) & 1);
setHighLow(s3, (val >> 3) & 1);
setHighLow(s4, (val >> 4) & 1);
setHighLow(s5, (val >> 5) & 1);
setHighLow(s6, (val >> 6) & 1);
setHighLow(s7, (val >> 7) & 1);
}
void
setP(uint8_t val)
{
setHighLow(p0, (val >> 0) & 1);
setHighLow(p1, (val >> 1) & 1);
setHighLow(p2, (val >> 2) & 1);
setHighLow(p3, (val >> 3) & 1);
setHighLow(p4, (val >> 4) & 1);
setHighLow(p5, (val >> 5) & 1);
setHighLow(p6, (val >> 6) & 1);
setHighLow(p7, (val >> 7) & 1);
}
void
setNOTIR(uint8_t val)
{
setHighLow(notir0, (val >> 0) & 1);
setHighLow(notir1, (val >> 1) & 1);
setHighLow(notir2, (val >> 2) & 1);
setHighLow(notir3, (val >> 3) & 1);
setHighLow(notir4, (val >> 4) & 1);
setHighLow(notir5, (val >> 5) & 1);
setHighLow(notir6, (val >> 6) & 1);
setHighLow(notir7, (val >> 7) & 1);
}
void
setPCL(uint8_t val)
{
setHighLow(pcl0, (val >> 0) & 1);
setHighLow(pcl1, (val >> 1) & 1);
setHighLow(pcl2, (val >> 2) & 1);
setHighLow(pcl3, (val >> 3) & 1);
setHighLow(pcl4, (val >> 4) & 1);
setHighLow(pcl5, (val >> 5) & 1);
setHighLow(pcl6, (val >> 6) & 1);
setHighLow(pcl7, (val >> 7) & 1);
}
void
setPCH(uint8_t val)
{
setHighLow(pch0, (val >> 0) & 1);
setHighLow(pch1, (val >> 1) & 1);
setHighLow(pch2, (val >> 2) & 1);
setHighLow(pch3, (val >> 3) & 1);
setHighLow(pch4, (val >> 4) & 1);
setHighLow(pch5, (val >> 5) & 1);
setHighLow(pch6, (val >> 6) & 1);
setHighLow(pch7, (val >> 7) & 1);
}
void
setPC(uint16_t val)
{
setPCL(val & 0xFF);
setPCH(val >> 8);
}
2010-09-22 05:14:57 +00:00
uint8_t
readA()
{
return (isNodeHigh(a0) << 0) |
(isNodeHigh(a1) << 1) |
(isNodeHigh(a2) << 2) |
(isNodeHigh(a3) << 3) |
(isNodeHigh(a4) << 4) |
(isNodeHigh(a5) << 5) |
(isNodeHigh(a6) << 6) |
(isNodeHigh(a7) << 7);
}
uint8_t
readX()
{
return (isNodeHigh(x0) << 0) |
(isNodeHigh(x1) << 1) |
(isNodeHigh(x2) << 2) |
(isNodeHigh(x3) << 3) |
(isNodeHigh(x4) << 4) |
(isNodeHigh(x5) << 5) |
(isNodeHigh(x6) << 6) |
(isNodeHigh(x7) << 7);
}
uint8_t
readY()
{
return (isNodeHigh(y0) << 0) |
(isNodeHigh(y1) << 1) |
(isNodeHigh(y2) << 2) |
(isNodeHigh(y3) << 3) |
(isNodeHigh(y4) << 4) |
(isNodeHigh(y5) << 5) |
(isNodeHigh(y6) << 6) |
(isNodeHigh(y7) << 7);
}
uint8_t
readP()
{
return (isNodeHigh(p0) << 0) |
(isNodeHigh(p1) << 1) |
(isNodeHigh(p2) << 2) |
(isNodeHigh(p3) << 3) |
(isNodeHigh(p4) << 4) |
(isNodeHigh(p5) << 5) |
(isNodeHigh(p6) << 6) |
(isNodeHigh(p7) << 7);
}
2010-09-22 15:25:34 +00:00
uint8_t
readNOTIR()
{
return (isNodeHigh(notir0) << 0) |
(isNodeHigh(notir1) << 1) |
(isNodeHigh(notir2) << 2) |
(isNodeHigh(notir3) << 3) |
(isNodeHigh(notir4) << 4) |
(isNodeHigh(notir5) << 5) |
(isNodeHigh(notir6) << 6) |
(isNodeHigh(notir7) << 7);
}
2010-09-22 05:14:57 +00:00
uint8_t
readSP()
{
return (isNodeHigh(s0) << 0) |
(isNodeHigh(s1) << 1) |
(isNodeHigh(s2) << 2) |
(isNodeHigh(s3) << 3) |
(isNodeHigh(s4) << 4) |
(isNodeHigh(s5) << 5) |
(isNodeHigh(s6) << 6) |
(isNodeHigh(s7) << 7);
}
uint8_t
readPCL()
{
return (isNodeHigh(pcl0) << 0) |
(isNodeHigh(pcl1) << 1) |
(isNodeHigh(pcl2) << 2) |
(isNodeHigh(pcl3) << 3) |
(isNodeHigh(pcl4) << 4) |
(isNodeHigh(pcl5) << 5) |
(isNodeHigh(pcl6) << 6) |
(isNodeHigh(pcl7) << 7);
}
uint8_t
readPCH()
{
return (isNodeHigh(pch0) << 0) |
(isNodeHigh(pch1) << 1) |
(isNodeHigh(pch2) << 2) |
(isNodeHigh(pch3) << 3) |
(isNodeHigh(pch4) << 4) |
(isNodeHigh(pch5) << 5) |
(isNodeHigh(pch6) << 6) |
(isNodeHigh(pch7) << 7);
}
uint16_t
readPC()
{
return (readPCH() << 8) | readPCL();
2010-09-22 01:51:38 +00:00
}
void
chipStatus()
{
2010-09-22 15:25:34 +00:00
printf("halfcyc:%d phi0:%d AB:%04X D:%02X RnW:%d PC:%04X A:%02X X:%02X Y:%02X SP:%02X P:%02X IR:%02X\n",
2010-09-22 05:14:57 +00:00
cycle,
isNodeHigh(clk0),
readAddressBus(),
readDataBus(),
isNodeHigh(rw),
readPC(),
readA(),
readX(),
readY(),
readSP(),
2010-09-22 15:25:34 +00:00
readP(),
readNOTIR() ^ 0xFF);
2010-09-22 05:14:57 +00:00
#if 0
var machine1 =
' halfcyc:' + cycle +
' phi0:' + readBit('clk0') +
' AB:' + hexWord(ab) +
' D:' + hexByte(readDataBus()) +
' RnW:' + readBit('rw');
var machine2 =
' PC:' + hexWord(readPC()) +
' A:' + hexByte(readA()) +
' X:' + hexByte(readX()) +
' Y:' + hexByte(readY()) +
' SP:' + hexByte(readSP()) +
' ' + readPstring();
var machine3 =
' Sync:' + readBit('sync')
' IRQ:' + readBit('irq') +
' NMI:' + readBit('nmi');
var machine4 =
' IR:' + hexByte(255 - readBits('notir', 8)) +
' idl:' + hexByte(255 - readBits('idl', 8)) +
' alu:' + hexByte(255 - readBits('alu', 8)) +
' TCstate:' + readBit('clock1') + readBit('clock2') +
readBit('t2') + readBit('t3') + readBit('t4') + readBit('t5');
var machine5 =
' notRdy0:' + readBit('notRdy0') +
' fetch:' + readBit('fetch') +
' clearIR:' + readBit('clearIR') +
' D1x1:' + readBit('D1x1');
setStatus(machine1 + "<br>" + machine2);
if (loglevel>2 && ctrace) {
console.log(machine1 + " " + machine2 + " " + machine3 + " " + machine4 + " " + machine5);
}
#endif
2010-09-22 01:51:38 +00:00
}
void
halfStep()
{
2010-09-22 05:14:57 +00:00
#ifdef DEBUG
printf("%s\n", __func__);
#endif
2010-09-22 01:51:38 +00:00
if (isNodeHigh(clk0)) {
setLow(clk0);
handleBusRead();
} else {
setHigh(clk0);
handleBusWrite();
}
}
void
initChip()
{
2010-09-22 05:14:57 +00:00
#ifdef DEBUG
printf("%s\n", __func__);
#endif
2010-09-22 22:57:00 +00:00
nodenum_t nn;
2010-09-22 07:35:22 +00:00
for (nn = 0; nn < NODES; nn++)
2010-09-22 01:51:38 +00:00
nodes[nn].state = STATE_FL;
nodes[ngnd].state = STATE_GND;
nodes[npwr].state = STATE_VCC;
2010-09-22 23:02:36 +00:00
transnum_t tn;
2010-09-22 07:35:22 +00:00
for (tn = 0; tn < TRANSISTORS; tn++)
set_transistors_on(tn, NO);
2010-09-22 01:51:38 +00:00
setLow(res);
setLow(clk0);
setHigh(rdy);
setLow(so);
setHigh(irq);
setHigh(nmi);
recalcAllNodes();
#if 0
var string = '';
for (var i in nodes) {
string += ' '+nodes[i].pullup;
}
console.log(string);
string = '';
for (var i in transistors) {
string += ' '+transistors_on[i];
2010-09-22 01:51:38 +00:00
}
console.log(string);
#endif
#if 1
2010-09-22 01:51:38 +00:00
int i;
for (i = 0; i < 8; i++) {
setHigh(clk0);
setLow(clk0);
}
#endif
2010-09-22 01:51:38 +00:00
setHigh(res);
#if 0
2010-09-22 01:51:38 +00:00
for (i = 0; i < 18; i++)
halfStep();
#endif
2010-09-22 01:51:38 +00:00
cycle = 0;
// chipStatus();
}
void
step()
{
2010-09-22 05:14:57 +00:00
#ifdef DEBUG
2010-09-22 01:51:38 +00:00
printf("%s\n", __func__);
2010-09-22 05:14:57 +00:00
#endif
2010-09-22 01:51:38 +00:00
halfStep();
cycle++;
2010-09-22 15:25:34 +00:00
if (verbose)
chipStatus();
PC = readPC();
if (PC >= 0xFF90 && ((PC - 0xFF90) % 3 == 0) && isNodeHigh(clk0)) {
2010-09-22 15:25:34 +00:00
A = readA();
X = readX();
Y = readY();
S = readSP();
P = readP();
N = P >> 7;
Z = (P >> 1) & 1;
C = P & 1;
#if 1
kernal_dispatch();
/*
LDA #P
PHA
LDA #A
LDX #X
LDY #Y
RTI
*/
memory[0xf800] = 0xA9;
memory[0xf801] = P;
memory[0xf802] = 0x48;
memory[0xf803] = 0xA9;
memory[0xf804] = A;
memory[0xf805] = 0xA2;
memory[0xf806] = X;
memory[0xf807] = 0xA0;
memory[0xf808] = Y;
memory[0xf809] = 0x40;
#if 0
PC = memory[0x0100 + S+1] | memory[0x0100 + S + 2] << 8;
PC++;
S += 2;
P &= 0x7C; // clear N, Z, C
P |= (N << 7) | (Z << 1) | C;
setA(A);
setX(X);
setY(Y);
// setP(P);
// recalcAllNodes();
#endif
#endif
}
2010-09-22 01:51:38 +00:00
}
void
steps()
{
2010-09-22 05:14:57 +00:00
#ifdef DEBUG
printf("%s\n", __func__);
#endif
2010-09-22 01:51:38 +00:00
for (;;)
step();
}
void
go(n)
{
2010-09-22 05:14:57 +00:00
#ifdef DEBUG
printf("%s\n", __func__);
#endif
#if 0
2010-09-22 01:51:38 +00:00
memcpy(memory, code, sizeof(code));
memory[0xfffc] = 0x00;
memory[0xfffd] = 0x00;
#else
FILE *f;
f = fopen("cbmbasic.bin", "r");
fread(memory + 0xA000, 1, 17591, f);
fclose(f);
2010-09-22 15:25:34 +00:00
// memset(memory + 0xFF90, 0x60, 0x70);
int addr;
for (addr = 0xFF90; addr < 0x10000; addr += 3) {
memory[addr+0] = 0x4C;
memory[addr+1] = 0x00;
memory[addr+2] = 0xF8;
}
#if 0
memory[0xfffc] = 0x94;
memory[0xfffd] = 0xE3;
2010-09-22 15:25:34 +00:00
#else
memory[0xf000] = 0x20;
memory[0xf001] = 0x94;
memory[0xf002] = 0xE3;
memory[0xfffc] = 0x00;
memory[0xfffd] = 0xF0;
#endif
#endif
2010-09-22 01:51:38 +00:00
steps();
}
void
setup()
{
setupNodes();
setupTransistors();
initChip();
go();
}
int
main()
{
setup();
return 0;
}