1
0
mirror of https://github.com/mist64/perfect6502.git synced 2024-06-01 10:41:28 +00:00
perfect6502/perfect6502.c

799 lines
16 KiB
C
Raw Normal View History

2010-09-22 07:35:22 +00:00
//#define DEBUG
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;
#define NO 0
#define YES 1
#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_UNDEFINED,
STATE_GND,
STATE_VCC,
STATE_FL,
STATE_FH,
STATE_PD,
STATE_PU
};
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;
int state;
2010-09-22 07:52:19 +00:00
int gates[NODES];
int c1c2s[2*NODES];
2010-09-22 01:51:38 +00:00
int gatecount;
int c1c2count;
} 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 01:51:38 +00:00
int name;
BOOL on;
int gate;
int c1;
int c2;
2010-09-22 05:14:57 +00:00
} transistor_t;
2010-09-22 07:35:22 +00:00
transistor_t transistors[TRANSISTORS];
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()
{
int i;
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()
{
int i;
for (i = 0; i < sizeof(transdefs)/sizeof(*transdefs); i++) {
int gate = transdefs[i].gate;
int c1 = transdefs[i].c1;
int c2 = transdefs[i].c2;
transistors[i].name = i;
transistors[i].on = NO;
transistors[i].gate = gate;
transistors[i].c1 = c1;
transistors[i].c2 = c2;
2010-09-22 07:52:19 +00:00
// printf("1 gate=%d, gatecount=%d\n", gate, nodes[gate].gatecount);
2010-09-22 01:51:38 +00:00
nodes[gate].gates[nodes[gate].gatecount++] = i;
2010-09-22 07:52:19 +00:00
// printf("2 gate=%d, gatecount=%d\n", gate, nodes[gate].gatecount);
if (nodes[gate].gatecount > NODES)
printf("0BIG\n");
2010-09-22 01:51:38 +00:00
nodes[c1].c1c2s[nodes[c1].c1c2count++] = i;
nodes[c2].c1c2s[nodes[c2].c1c2count++] = i;
2010-09-22 07:52:19 +00:00
// printf("3 gate=%d, c1c2count=%d\n", gate, nodes[gate].c1c2count);
if (nodes[gate].c1c2count > 2*NODES)
printf("1BIG\n");
2010-09-22 01:51:38 +00:00
}
}
2010-09-22 05:14:57 +00:00
#ifdef DEBUG
void
printarray(int *array, int count)
{
int i;
for (i = 0; i < count; i++)
printf("%d ", array[i]);
printf("\n");
}
#endif
2010-09-22 01:51:38 +00:00
BOOL
arrayContains(int *arr, int count, int el)
{
2010-09-22 05:14:57 +00:00
#ifdef DEBUG
printf("%s el=%d, arr=", __func__, el);
printarray(arr, count);
#endif
2010-09-22 01:51:38 +00:00
int i;
for (i = 0; i < count; i++) {
2010-09-22 05:14:57 +00:00
if (arr[i] == el) {
#ifdef DEBUG
printf("YES\n");
#endif
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
}
2010-09-22 05:14:57 +00:00
#ifdef DEBUG
printf("NO\n");
#endif
2010-09-22 01:51:38 +00:00
return NO;
}
void addNodeToGroup(int i, int *group, int *groupcount);
void
addNodeTransistor(int node, int t, int *group, int *groupcount)
{
2010-09-22 05:14:57 +00:00
#ifdef DEBUG
printf("%s n=%d, t=%d, group=", __func__, node, t);
printarray(group, *groupcount);
#endif
2010-09-22 01:51:38 +00:00
if (!transistors[t].on)
return;
int other;
if (transistors[t].c1 == node)
other = transistors[t].c2;
if (transistors[t].c2 == node)
other = transistors[t].c1;
addNodeToGroup(other, group, groupcount);
}
void
addNodeToGroup(int i, int *group, int *groupcount)
{
2010-09-22 05:14:57 +00:00
#ifdef DEBUG
printf("%s %d, group=", __func__, i);
printarray(group, *groupcount);
#endif
2010-09-22 01:51:38 +00:00
if (arrayContains(group, *groupcount, i))
return;
2010-09-22 05:14:57 +00:00
group[*groupcount] = i;
(*groupcount)++;
2010-09-22 01:51:38 +00:00
if (i == ngnd)
return;
if (i == npwr)
return;
int t;
for (t = 0; t < nodes[i].c1c2count; t++)
addNodeTransistor(i, nodes[i].c1c2s[t], group, groupcount);
}
int
getNodeValue(int *group, int groupcount)
{
2010-09-22 05:14:57 +00:00
#ifdef DEBUG
printf("%s group=", __func__);
printarray(group, groupcount);
#endif
2010-09-22 01:51:38 +00:00
if (arrayContains(group, groupcount, ngnd))
return STATE_GND;
if (arrayContains(group, groupcount, npwr))
return STATE_VCC;
2010-09-22 08:28:34 +00:00
int flstate = STATE_UNDEFINED;
2010-09-22 01:51:38 +00:00
int i;
for (i = 0; i < groupcount; i++) {
int nn = group[i];
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
addRecalcNode(int nn, int *recalclist, int *recalccount)
{
#ifdef DEBUG
printf("%s nn=%d recalclist=", __func__, nn);
printarray(recalclist, *recalccount);
#endif
if (nn == ngnd)
return;
if (nn == npwr)
return;
if (arrayContains(recalclist, *recalccount, nn))
return;
recalclist[*recalccount] = nn;
(*recalccount)++;
}
void
floatnode(int nn)
{
#ifdef DEBUG
printf("%s nn=%d\n", __func__, nn);
#endif
if (nn == ngnd)
return;
if (nn == npwr)
return;
node_t n = nodes[nn];
if (n.state == STATE_GND)
2010-09-22 07:25:32 +00:00
nodes[nn].state = STATE_FL;
2010-09-22 05:14:57 +00:00
if (n.state == STATE_PD)
2010-09-22 07:25:32 +00:00
nodes[nn].state = STATE_FL;
2010-09-22 05:14:57 +00:00
if (n.state == STATE_VCC)
2010-09-22 07:25:32 +00:00
nodes[nn].state = STATE_FH;
2010-09-22 05:14:57 +00:00
if (n.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
}
void
2010-09-22 07:25:32 +00:00
turnTransistorOn(transistor_t *t, int *recalclist, int *recalccount)
2010-09-22 05:14:57 +00:00
{
2010-09-22 07:25:32 +00:00
if (t->on)
2010-09-22 05:14:57 +00:00
return;
#ifdef DEBUG
2010-09-22 07:25:32 +00:00
printf("%s t%d, %d, %d, %d\n", __func__, t->name, t->gate, t->c1, t->c2);
2010-09-22 05:14:57 +00:00
#endif
2010-09-22 07:25:32 +00:00
t->on = YES;
addRecalcNode(t->c1, recalclist, recalccount);
addRecalcNode(t->c2, recalclist, recalccount);
2010-09-22 05:14:57 +00:00
}
void
2010-09-22 07:25:32 +00:00
turnTransistorOff(transistor_t *t, int *recalclist, int *recalccount)
2010-09-22 05:14:57 +00:00
{
2010-09-22 07:41:26 +00:00
if (!t->on)
2010-09-22 05:14:57 +00:00
return;
#ifdef DEBUG
2010-09-22 07:25:32 +00:00
printf("%s t%d, %d, %d, %d\n", __func__, t->name, t->gate, t->c1, t->c2);
2010-09-22 05:14:57 +00:00
#endif
2010-09-22 07:25:32 +00:00
t->on = NO;
floatnode(t->c1);
floatnode(t->c2);
addRecalcNode(t->c1, recalclist, recalccount);
addRecalcNode(t->c2, recalclist, recalccount);
2010-09-22 05:14:57 +00:00
}
BOOL
isNodeHigh(int nn)
{
#ifdef DEBUG
2010-09-22 07:25:32 +00:00
printf("%s nn=%d state=%d\n", __func__, nn, nodes[nn].state);
printf("%s nn=%d res=%d\n", __func__, nn, (nodes[nn].state == STATE_VCC) ||
(nodes[nn].state == STATE_PU) ||
(nodes[nn].state == STATE_FH));
2010-09-22 05:14:57 +00:00
#endif
2010-09-22 07:25:32 +00:00
//printf("%s nn=%d res=%d\n", __func__, nn, nodes[nn].state);
2010-09-22 05:14:57 +00:00
return ((nodes[nn].state == STATE_VCC) ||
(nodes[nn].state == STATE_PU) ||
(nodes[nn].state == STATE_FH));
}
void
recalcTransistor(int tn, int *recalclist, int *recalccount)
{
#ifdef DEBUG
printf("%s tn=%d, recalclist=", __func__, tn);
printarray(recalclist, *recalccount);
#endif
2010-09-22 07:25:32 +00:00
transistor_t *t = &transistors[tn];
if (isNodeHigh(t->gate))
2010-09-22 05:14:57 +00:00
turnTransistorOn(t, recalclist, recalccount);
else
turnTransistorOff(t, recalclist, recalccount);
}
2010-09-22 01:51:38 +00:00
void
recalcNode(int node, int *recalclist, int *recalccount)
{
2010-09-22 05:14:57 +00:00
#ifdef DEBUG
printf("%s node=%d, recalclist=", __func__, node);
printarray(recalclist, *recalccount);
#endif
2010-09-22 01:51:38 +00:00
if (node == ngnd)
return;
if (node == npwr)
return;
2010-09-22 05:14:57 +00:00
int group[2000];
2010-09-22 01:51:38 +00:00
int groupcount = 0;
addNodeToGroup(node, group, &groupcount);
2010-09-22 05:14:57 +00:00
int newv = getNodeValue(group, groupcount);
int i;
#ifdef DEBUG
printf("%s %i, group=", __func__, node);
printarray(group, groupcount);
#endif
for (i = 0; i < groupcount; i++) {
2010-09-22 07:52:19 +00:00
//printf("i=%d\n", i);
//printf("group[i]=%d\n", group[i]);
2010-09-22 05:14:57 +00:00
node_t n = nodes[group[i]];
#ifdef DEBUG
if (n.state != newv)
printf("%s %d, states %d,%d\n", __func__, group[i], n.state, newv);
#endif
2010-09-22 07:25:32 +00:00
nodes[group[i]].state = newv;
2010-09-22 05:14:57 +00:00
int t;
#ifdef DEBUG
printf("loop x %d\n", n.gatecount);
2010-09-22 01:51:38 +00:00
#endif
2010-09-22 07:52:19 +00:00
//printf("there are %d gates\n", n.gatecount);
2010-09-22 05:14:57 +00:00
for (t = 0; t < n.gatecount; t++)
recalcTransistor(n.gates[t], recalclist, recalccount);
}
2010-09-22 01:51:38 +00:00
}
void
recalcNodeList(int *list, int count)
{
2010-09-22 05:14:57 +00:00
#ifdef DEBUG
printf("%s list=", __func__);
printarray(list, count);
#endif
int recalclist[2000];
2010-09-22 01:51:38 +00:00
int recalccount = 0;
int i, j;
for (j = 0; j < 100; j++) { // loop limiter
if (!count)
return;
2010-09-22 05:14:57 +00:00
#ifdef DEBUG
printf("%s iteration=%d, list=", __func__, j);
printarray(list, count);
#endif
2010-09-22 07:41:26 +00:00
//printf("%s:%d iteration=%d count=%d\n", __func__, __LINE__, j, count);
2010-09-22 07:35:22 +00:00
//printf("before: %d\n", recalccount);
2010-09-22 01:51:38 +00:00
for (i = 0; i < count; i++)
recalcNode(list[i], recalclist, &recalccount);
2010-09-22 07:41:26 +00:00
//printf("%s:%d iteration=%d recalccount=%d\n", __func__, __LINE__, j, recalccount);
2010-09-22 07:35:22 +00:00
//printf("after: %d\n", recalccount);
2010-09-22 01:51:38 +00:00
for (i = 0; i < recalccount; i++)
list[i] = recalclist[i];
2010-09-22 07:41:26 +00:00
//printf("%s:%d iteration=%d\n", __func__, __LINE__, j);
2010-09-22 01:51:38 +00:00
count = recalccount;
recalccount = 0;
}
}
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);
int list[NODES];
2010-09-22 05:14:57 +00:00
int 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
setLow(int nn)
{
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 07:35:22 +00:00
int list[NODES];
list[0] = nn;
recalcNodeList(list, 1);
2010-09-22 01:51:38 +00:00
}
void
setHigh(int nn)
{
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 07:35:22 +00:00
int list[NODES];
list[0] = nn;
recalcNodeList(list, 1);
2010-09-22 01:51:38 +00:00
}
2010-09-22 05:14:57 +00:00
void
writeDataBus(uint8_t x)
2010-09-22 01:51:38 +00:00
{
2010-09-22 07:35:22 +00:00
int recalcs[NODES];
2010-09-22 05:14:57 +00:00
int recalcscount = 0;
int i;
for (i = 0; i < 8; i++) {
int nn;
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)
{
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 08:28:34 +00:00
printf("POKE $0x%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);
}
}
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);
}
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 10:05:49 +00:00
//exit(1);
PC = readPC();
printf("PC = %x\n", PC);
if (PC >= 0xFF90) {
A = readA();
X = readX();
Y = readY();
S = readSP();
P = readP();
N = P >> 7;
Z = (P >> 1) & 1;
C = P & 1;
kernal_dispatch();
}
2010-09-22 05:14:57 +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\n",
cycle,
isNodeHigh(clk0),
readAddressBus(),
readDataBus(),
isNodeHigh(rw),
readPC(),
readA(),
readX(),
readY(),
readSP(),
readP());
#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 01:51:38 +00:00
int 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;
int tn;
2010-09-22 07:35:22 +00:00
for (tn = 0; tn < TRANSISTORS; tn++)
2010-09-22 01:51:38 +00:00
transistors[tn].on = NO;
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[i].on;
}
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++;
chipStatus();
}
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);
memory[0xfffc] = 0x94;
memory[0xfffd] = 0xE3;
#endif
2010-09-22 01:51:38 +00:00
steps();
}
void
setup()
{
setupNodes();
setupTransistors();
initChip();
go();
}
int
main()
{
setup();
return 0;
}