ref: e86160e1c5cae5d7ac79851a7167a1a0a3a6eddb
parent: 6ca6987e90f6757a354c0be220a4bd6c9e47d64a
author: Alex Musolino <[email protected]>
date: Thu May 11 12:34:30 EDT 2023
rudimentary display support, drop debugging output
--- a/8080.c
+++ b/8080.c
@@ -2,6 +2,8 @@
#include <libc.h>
#include <bio.h>
#include <ctype.h>
+#include <draw.h>
+#include <thread.h>
#include "dat.h"
#include "fns.h"
@@ -12,10 +14,15 @@
CPU ocpu, cpu;
Insn insn;
+enum{
+ Width = 256,
+ Height = 224,
+};
+
uchar mem[MEMSZ];
uchar *rom = &mem[0];
uchar *ram = &mem[ROMSZ];
-uchar *vid = &mem[ROMSZ+RAMSZ];
+uchar *vmem = &mem[ROMSZ+RAMSZ];
int interactive;
int debug;
@@ -29,7 +36,7 @@
usage(void)
{
fprint(2, "usage: %s [-b addr] [-i script] [-t addr] [-T addr]\n", argv0);
- exits("usage");
+ threadexitsall("usage");
}
int
@@ -284,7 +291,7 @@
}
break;
case Cexit:
- exits(0);
+ threadexitsall(0);
break;
case Creset:
cpureset();
@@ -352,8 +359,52 @@
return strcmp(buf, "/dev/cons") == 0;
}
+static void
+resize(int w, int h)
+{
+ int fd;
+
+ if((fd = open("/dev/wctl", OWRITE)) < 0)
+ return;
+ fprint(fd, "resize -dx %d -dy %d", w, h);
+ close(fd);
+}
+
+static void
+scanout(void*)
+{
+ int i;
+ uchar *p;
+ Image *line;
+ Rectangle r;
+
+ line = allocimage(display, Rect(0,0,Width,1), GREY1, 0, DNofill);
+
+ for(;;){
+ p = vmem;
+ r.min = Pt(screen->r.min.x, screen->r.max.y-1);
+ r.max = Pt(screen->r.max.x, screen->r.max.y);
+ for(i = 0; i < Height/2; i++){
+ loadimage(line, Rect(0,0,Width,1), p, Width/8);
+ draw(screen, r, line, nil, ZP);
+ r = rectaddpt(r, Pt(0, -1));
+ p += Width/8;
+ }
+ flushimage(display, 1);
+ sleep(1000/30);
+ for(; i < Height; i++){
+ loadimage(line, Rect(0,0,Width,1), p, Width/8);
+ draw(screen, r, line, nil, ZP);
+ r = rectaddpt(r, Pt(0, -1));
+ p += Width/8;
+ }
+ flushimage(display, 1);
+ sleep(1000/30);
+ }
+}
+
void
-main(int argc, char **argv)
+threadmain(int argc, char **argv)
{
stdin = Bfdopen(0, OREAD);
stdout = Bfdopen(1, OWRITE);
@@ -386,6 +437,12 @@
}ARGEND;
if(argc != 0)
usage();
+ if(newwindow(nil) < 0)
+ sysfatal("newwindow: %r");
+ resize(Width, Height);
+ if(initdraw(nil, nil, nil) < 0)
+ sysfatal("initdraw: %r");
+ proccreate(scanout, nil, 1024);
interactive = isatty();
fmtinstall('I', insnfmt);
cpureset();
@@ -398,7 +455,7 @@
case -1:
sysfatal("error reading stdin: %r");
case 0:
- exits(0);
+ threadexitsall(0);
}
}
}
--- a/das.c
+++ b/das.c
@@ -290,7 +290,7 @@
{
insn->r1 = (mem[0]>>3)&0x7;
insn->r2 = mem[0]&0x7;
- if(insn->r1 == M && insn->r2 == M)
+ if(insn->r1 == RM && insn->r2 == RM)
insn->op = Onop;
return 1;
}
@@ -573,18 +573,10 @@
static u16int
rpair(CPU *cpu, u8int rp)
{
- u16int x;
-
switch(rp){
- case BC: return cpu->r[B]<<8 | cpu->r[C];
- case DE: return cpu->r[D]<<8 | cpu->r[E];
- case HL:
- x = cpu->r[H]<<8 | cpu->r[L];
- if(rp == HL && x == 0){
- Bprint(stderr, "HL = 0 @ pc=%#.4uhx\n", cpu->PC);
- Bflush(stderr);
- }
- return x;
+ case RBC: return cpu->r[RB]<<8 | cpu->r[RC];
+ case RDE: return cpu->r[RD]<<8 | cpu->r[RE];
+ case RHL: return cpu->r[RH]<<8 | cpu->r[RL];
}
fatal("unknown register pair %d", rp);
return 0;
@@ -593,12 +585,8 @@
static void
wpair(CPU *cpu, u8int rp, u16int x)
{
- if(rp == HL && x == 0){
- Bprint(stderr, "HL ← 0 @ pc=%#.4uhx\n", cpu->PC);
- Bflush(stderr);
- }
- cpu->r[(rp<<1)+B] = x>>8;
- cpu->r[(rp<<1)+B+1] = x;
+ cpu->r[(rp<<1)] = x>>8;
+ cpu->r[(rp<<1)+1] = x;
}
static void
@@ -633,8 +621,8 @@
{
u16int addr;
- if(insn->r1 == M){
- addr = rpair(cpu, HL);
+ if(insn->r1 == RM){
+ addr = rpair(cpu, RHL);
memwrite(addr, insn->imm);
}else{
cpu->r[insn->r1] = insn->imm;
@@ -644,8 +632,6 @@
static void
Xcall(CPU *cpu, Insn *insn)
{
- //Bprint(stderr, "CALL %#.4uhx @ pc=%#.4uhx\n", insn->addr, insn->pc);
- //Bflush(stderr);
push16(cpu, cpu->PC);
cpu->PC = insn->addr;
}
@@ -653,18 +639,18 @@
static void
Xldax(CPU *cpu, Insn *insn)
{
- cpu->r[A] = memread(rpair(cpu, insn->rp));
+ cpu->r[RA] = memread(rpair(cpu, insn->rp));
}
static void
Xmov(CPU *cpu, Insn *insn)
{
- if(insn->r2 == M && insn->r1 == M)
+ if(insn->r2 == RM && insn->r1 == RM)
return;
- if(insn->r2 == M)
- cpu->r[insn->r1] = memread(rpair(cpu, HL));
- else if(insn->r1 == M)
- memwrite(rpair(cpu, HL), cpu->r[insn->r2]);
+ if(insn->r2 == RM)
+ cpu->r[insn->r1] = memread(rpair(cpu, RHL));
+ else if(insn->r1 == RM)
+ memwrite(rpair(cpu, RHL), cpu->r[insn->r2]);
else
cpu->r[insn->r1] = cpu->r[insn->r2];
}
@@ -680,8 +666,8 @@
{
u16int a, x;
- if(insn->r1 == M){
- a = rpair(cpu, HL);
+ if(insn->r1 == RM){
+ a = rpair(cpu, RHL);
x = memread(a);
}else{
a = 0;
@@ -698,7 +684,7 @@
else
cpu->flg &= ~Fsign;
- if(insn->r1 == M)
+ if(insn->r1 == RM)
memwrite(a, x);
else
cpu->r[insn->r1] = x;
@@ -720,18 +706,18 @@
}else{
x = rpair(cpu, insn->rp);
}
- x += rpair(cpu, HL);
+ x += rpair(cpu, RHL);
if(x>>16 > 0)
cpu->flg |= Fcarry;
else
cpu->flg &= ~Fcarry;
- wpair(cpu, HL, x);
+ wpair(cpu, RHL, x);
}
static void
Xsta(CPU *cpu, Insn *insn)
{
- memwrite(insn->addr, cpu->r[A]);
+ memwrite(insn->addr, cpu->r[RA]);
}
static void
@@ -739,19 +725,19 @@
{
u8int x;
- if(insn->r1 == M)
- x = memread(rpair(cpu, HL));
+ if(insn->r1 == RM)
+ x = memread(rpair(cpu, RHL));
else
x = cpu->r[insn->r1];
- cpu->r[A] ^= x;
+ cpu->r[RA] ^= x;
- if(cpu->r[A] == 0)
+ if(cpu->r[RA] == 0)
cpu->flg |= Fzero;
else
cpu->flg &= ~Fzero;
- if((cpu->r[A] & 0x80) != 0)
+ if((cpu->r[RA] & 0x80) != 0)
cpu->flg |= Fsign;
else
cpu->flg &= ~Fsign;
@@ -762,7 +748,7 @@
static void
Xout(CPU *cpu, Insn *insn)
{
- iow(insn->imm<<8|insn->imm, cpu->r[A]);
+ iow(insn->imm<<8|insn->imm, cpu->r[RA]);
}
static void
@@ -770,19 +756,19 @@
{
u8int x;
- if(insn->r1 == M)
- x = memread(rpair(cpu, HL));
+ if(insn->r1 == RM)
+ x = memread(rpair(cpu, RHL));
else
x = cpu->r[insn->r1];
- cpu->r[A] |= x;
+ cpu->r[RA] |= x;
- if(cpu->r[A] == 0)
+ if(cpu->r[RA] == 0)
cpu->flg |= Fzero;
else
cpu->flg &= ~Fzero;
- if((cpu->r[A] & 0x80) != 0)
+ if((cpu->r[RA] & 0x80) != 0)
cpu->flg |= Fsign;
else
cpu->flg &= ~Fsign;
@@ -793,7 +779,7 @@
static void
Xlda(CPU *cpu, Insn *insn)
{
- cpu->r[A] = memread(insn->addr);
+ cpu->r[RA] = memread(insn->addr);
}
static void
@@ -801,19 +787,19 @@
{
u8int x;
- if(insn->r1 == M)
- x = memread(rpair(cpu, HL));
+ if(insn->r1 == RM)
+ x = memread(rpair(cpu, RHL));
else
x = cpu->r[insn->r1];
- cpu->r[A] &= x;
+ cpu->r[RA] &= x;
- if(cpu->r[A] == 0)
+ if(cpu->r[RA] == 0)
cpu->flg |= Fzero;
else
cpu->flg &= ~Fzero;
- if((cpu->r[A] & 0x80) != 0)
+ if((cpu->r[RA] & 0x80) != 0)
cpu->flg |= Fsign;
else
cpu->flg &= ~Fsign;
@@ -824,18 +810,11 @@
static void
Xpush(CPU *cpu, Insn *insn)
{
- u16int x;
-
- if(insn->rp == MM){
- push8(cpu, cpu->r[A]);
+ if(insn->rp == RMM){
+ push8(cpu, cpu->r[RA]);
push8(cpu, cpu->flg);
}else{
- x = rpair(cpu, insn->rp);
- if(insn->rp == HL && x == 0){
- Bprint(stderr, "pushed %uhd from %s @ %#.4uhx\n", x, rpnam(insn->rp), insn->pc);
- Bflush(stderr);
- }
- push16(cpu, x);
+ push16(cpu, rpair(cpu, insn->rp));
}
}
@@ -842,18 +821,11 @@
static void
Xpop(CPU *cpu, Insn *insn)
{
- u16int x;
-
- if(insn->rp == MM){
+ if(insn->rp == RMM){
cpu->flg = pop8(cpu);
- cpu->r[A] = pop8(cpu);
+ cpu->r[RA] = pop8(cpu);
}else{
- x = pop16(cpu);
- if(insn->rp == HL && x == 0){
- Bprint(stderr, "popped %uhd into %s @ %#.4uhx\n", x, rpnam(insn->rp), insn->pc);
- Bflush(stderr);
- }
- wpair(cpu, insn->rp, x);
+ wpair(cpu, insn->rp, pop16(cpu));
}
}
@@ -862,9 +834,9 @@
{
u16int x;
- x = rpair(cpu, HL);
- wpair(cpu, HL, rpair(cpu, DE));
- wpair(cpu, DE, x);
+ x = rpair(cpu, RHL);
+ wpair(cpu, RHL, rpair(cpu, RDE));
+ wpair(cpu, RDE, x);
}
static void
@@ -873,8 +845,8 @@
u8int x;
u16int a;
- if(insn->r1 == M){
- a = memread(rpair(cpu, HL));
+ if(insn->r1 == RM){
+ a = memread(rpair(cpu, RHL));
x = memread(a) + 1;
memwrite(a, x);
}else{
@@ -911,19 +883,19 @@
static void
Xani(CPU *cpu, Insn *insn)
{
- cpu->r[A] &= insn->imm;
+ cpu->r[RA] &= insn->imm;
- if(cpu->r[A] == 0)
+ if(cpu->r[RA] == 0)
cpu->flg |= Fzero;
else
cpu->flg &= ~Fzero;
- if((cpu->r[A] & 0x80) != 0)
+ if((cpu->r[RA] & 0x80) != 0)
cpu->flg |= Fsign;
else
cpu->flg &= ~Fsign;
- if(evenpar(cpu->r[A]))
+ if(evenpar(cpu->r[RA]))
cpu->flg |= Fparity;
else
cpu->flg &= ~Fparity;
@@ -937,24 +909,24 @@
u8int ocarry;
ocarry = (cpu->flg&Fcarry) != 0;
- if((cpu->r[A]&1) != 0)
+ if((cpu->r[RA]&1) != 0)
cpu->flg |= Fcarry;
else
cpu->flg &= ~Fcarry;
- cpu->r[A] = ocarry<<7|((cpu->r[A]>>1)&0x7f);
+ cpu->r[RA] = ocarry<<7|((cpu->r[RA]>>1)&0x7f);
}
static void
Xori(CPU *cpu, Insn *insn)
{
- cpu->r[A] |= insn->imm;
+ cpu->r[RA] |= insn->imm;
- if(cpu->r[A] == 0)
+ if(cpu->r[RA] == 0)
cpu->flg |= Fzero;
else
cpu->flg &= ~Fzero;
- if((cpu->r[A] & 0x80) != 0)
+ if((cpu->r[RA] & 0x80) != 0)
cpu->flg |= Fsign;
else
cpu->flg &= ~Fsign;
@@ -965,11 +937,11 @@
static void
Xcmp(CPU *cpu, Insn *insn)
{
- if(cpu->r[A] == insn->r1)
+ if(cpu->r[RA] == insn->r1)
cpu->flg |= Fzero;
else{
cpu->flg &= ~Fzero;
- if(cpu->r[A] < insn->r1)
+ if(cpu->r[RA] < insn->r1)
cpu->flg |= Fcarry;
else
cpu->flg &= ~Fcarry;
@@ -981,12 +953,12 @@
{
u8int ncarry;
- ncarry = (cpu->r[A]&0x80) != 0;
+ ncarry = (cpu->r[RA]&0x80) != 0;
if(ncarry != 0)
cpu->flg |= Fcarry;
else
cpu->flg &= ~Fcarry;
- cpu->r[A] = ((cpu->r[A]<<1)&0xfe)|ncarry;
+ cpu->r[RA] = ((cpu->r[RA]<<1)&0xfe)|ncarry;
}
static void
@@ -994,12 +966,12 @@
{
u8int ncarry;
- ncarry = cpu->r[A]&1;
+ ncarry = cpu->r[RA]&1;
if(ncarry != 0)
cpu->flg |= Fcarry;
else
cpu->flg &= ~Fcarry;
- cpu->r[A] = ncarry<<7|((cpu->r[A]>>1)&0x7f);
+ cpu->r[RA] = ncarry<<7|((cpu->r[RA]>>1)&0x7f);
}
static void
@@ -1014,17 +986,17 @@
static void
Xstax(CPU *cpu, Insn *insn)
{
- memwrite(rpair(cpu, insn->rp), cpu->r[A]);
+ memwrite(rpair(cpu, insn->rp), cpu->r[RA]);
}
static void
Xcpi(CPU *cpu, Insn *insn)
{
- if(cpu->r[A] == insn->imm)
+ if(cpu->r[RA] == insn->imm)
cpu->flg |= Fzero;
else{
cpu->flg &= ~Fzero;
- if(cpu->r[A] < insn->imm)
+ if(cpu->r[RA] < insn->imm)
cpu->flg |= Fcarry;
else
cpu->flg &= ~Fcarry;
@@ -1036,17 +1008,17 @@
{
u8int x, p;
- if(cpu->r[A] == 0){
+ if(cpu->r[RA] == 0){
if(f & Fzero) cpu->flg |= Fzero;
if(f & Fsign) cpu->flg &= ~Fsign;
}else{
if(f & Fzero) cpu->flg &= ~Fzero;
- if((cpu->r[A] & 0x80) != 0)
+ if((cpu->r[RA] & 0x80) != 0)
if(f & Fsign) cpu->flg |= Fsign;
}
if(f & Fparity){
- x = cpu->r[A];
+ x = cpu->r[RA];
p = 0;
p += (x&1); x >>= 1;
p += (x&1); x >>= 1;
@@ -1068,12 +1040,12 @@
{
u16int x;
- x = cpu->r[A] + insn->imm;
+ x = cpu->r[RA] + insn->imm;
if((x>>8) > 0)
cpu->flg |= Fcarry;
else
cpu->flg &= ~Fcarry;
- cpu->r[A] = x;
+ cpu->r[RA] = x;
psz(cpu, Fparity|Fsign|Fzero);
}
@@ -1092,7 +1064,7 @@
static void
Xin(CPU *cpu, Insn *insn)
{
- cpu->r[A] = ior(insn->addr);
+ cpu->r[RA] = ior(insn->addr);
}
static void
@@ -1157,7 +1129,7 @@
u8int a, b;
u16int x;
- a = cpu->r[A];
+ a = cpu->r[RA];
b = -insn->imm;
if((((a&0xf)+(b&0xf))&0x10) != 0)
cpu->flg |= Fhcarry;
@@ -1168,7 +1140,7 @@
cpu->flg &= ~Fcarry;
else
cpu->flg |= Fcarry;
- cpu->r[A] = x;
+ cpu->r[RA] = x;
psz(cpu, Fparity|Fsign|Fzero);
}
@@ -1178,17 +1150,17 @@
u8int x;
x = memread(cpu->SP+0);
- memwrite(cpu->SP+0, cpu->r[L]);
- cpu->r[L] = x;
+ memwrite(cpu->SP+0, cpu->r[RL]);
+ cpu->r[RL] = x;
x = memread(cpu->SP+1);
- memwrite(cpu->SP+1, cpu->r[H]);
- cpu->r[H] = x;
+ memwrite(cpu->SP+1, cpu->r[RH]);
+ cpu->r[RH] = x;
}
static void
Xpchl(CPU *cpu, Insn*)
{
- cpu->PC = rpair(cpu, HL);
+ cpu->PC = rpair(cpu, RHL);
}
static void
@@ -1205,22 +1177,22 @@
{
u16int x;
- if((cpu->flg&Fhcarry) != 0 || cpu->r[A] > 9){
- x = (cpu->r[A]&0xf)+6;
+ if((cpu->flg&Fhcarry) != 0 || cpu->r[RA] > 9){
+ x = (cpu->r[RA]&0xf)+6;
if(x>>4 > 0)
cpu->flg |= Fhcarry;
else
cpu->flg &= ~Fhcarry;
- cpu->r[A] = (cpu->r[A]&0xf0)|(x&0x0f);
+ cpu->r[RA] = (cpu->r[RA]&0xf0)|(x&0x0f);
}
- if((cpu->flg&Fcarry) != 0 || (cpu->r[A]>>4) > 9){
- x = (cpu->r[A]>>4)+6;
+ if((cpu->flg&Fcarry) != 0 || (cpu->r[RA]>>4) > 9){
+ x = (cpu->r[RA]>>4)+6;
if(x>>4 > 0)
cpu->flg |= Fcarry;
else
cpu->flg &= ~Fcarry;
- cpu->r[A] = (x<<4)|(cpu->r[A]&0xf);
+ cpu->r[RA] = (x<<4)|(cpu->r[RA]&0xf);
}
}
@@ -1241,6 +1213,6 @@
static void
Xshld(CPU *cpu, Insn *insn)
{
- memwrite(insn->addr+0, cpu->r[L]);
- memwrite(insn->addr+1, cpu->r[H]);
+ memwrite(insn->addr+0, cpu->r[RL]);
+ memwrite(insn->addr+1, cpu->r[RH]);
}
--- a/dat.h
+++ b/dat.h
@@ -16,21 +16,21 @@
};
enum{
- B,
- C,
- D,
- E,
- H,
- L,
- M, /* dummy */
- A,
+ RB,
+ RC,
+ RD,
+ RE,
+ RH,
+ RL,
+ RM, /* dummy */
+ RA,
};
enum{
- BC,
- DE,
- HL,
- MM,
+ RBC,
+ RDE,
+ RHL,
+ RMM,
};
enum{
--- a/debug.c
+++ b/debug.c
@@ -1,6 +1,7 @@
#include <u.h>
#include <libc.h>
#include <bio.h>
+#include <thread.h>
#include "dat.h"
#include "fns.h"
@@ -36,14 +37,14 @@
rnam(u8int r)
{
switch(r){
- case B: return "B";
- case C: return "C";
- case D: return "D";
- case E: return "E";
- case H: return "H";
- case L: return "L";
- case M: return "M";
- case A: return "A";
+ case RB: return "B";
+ case RC: return "C";
+ case RD: return "D";
+ case RE: return "E";
+ case RH: return "H";
+ case RL: return "L";
+ case RM: return "M";
+ case RA: return "A";
}
return "X";
}
@@ -52,9 +53,9 @@
rpnam(u8int r)
{
switch(r){
- case BC: return "BC";
- case DE: return "DE";
- case HL: return "HL";
+ case RBC: return "BC";
+ case RDE: return "DE";
+ case RHL: return "HL";
}
return "XX";
}
@@ -91,13 +92,13 @@
{
int i;
- Bprint(stderr, "A=%#.2x\n", cpu.r[A]);
- Bprint(stderr, "B=%#.2x\n", cpu.r[B]);
- Bprint(stderr, "C=%#.2x\n", cpu.r[C]);
- Bprint(stderr, "D=%#.2x\n", cpu.r[D]);
- Bprint(stderr, "E=%#.2x\n", cpu.r[E]);
- Bprint(stderr, "H=%#.2x\n", cpu.r[H]);
- Bprint(stderr, "L=%#.2x\n", cpu.r[L]);
+ Bprint(stderr, "A=%#.2x\n", cpu.r[RA]);
+ Bprint(stderr, "B=%#.2x\n", cpu.r[RB]);
+ Bprint(stderr, "C=%#.2x\n", cpu.r[RC]);
+ Bprint(stderr, "D=%#.2x\n", cpu.r[RD]);
+ Bprint(stderr, "E=%#.2x\n", cpu.r[RE]);
+ Bprint(stderr, "H=%#.2x\n", cpu.r[RH]);
+ Bprint(stderr, "L=%#.2x\n", cpu.r[RL]);
Bprint(stderr, "F=%#.2x", cpu.flg);
if(cpu.flg != 0){
Bprint(stderr, " (");
@@ -153,5 +154,5 @@
dumpinst0();
dumpregs0();
Bflush(stderr);
- exits("fatal");
+ threadexitsall("fatal");
}
--- a/screen.c
+++ b/screen.c
@@ -14,7 +14,7 @@
usage(void)
{
fprint(2, "usage: %s\n", argv0);
- exits("usage");
+ threadexitsall("usage");
}
static void
--- a/test.rc
+++ b/test.rc
@@ -1,7 +1,6 @@
#!/bin/rc
mk && 6.out -i <{cat <<EOF
load invaders.rom
-bpset 0x18dc
-bpset 0x18df
+run
EOF
}