ref: eee9501cb9a975d6b9c507db70feb338fa604b26
parent: fd4f5b1a9a7f81825a5023b251a2e788e986e49c
author: Alex Musolino <alex@musolino.id.au>
date: Mon May 8 22:47:54 EDT 2023
use libbio for input and output
--- a/8080.c
+++ b/8080.c
@@ -4,6 +4,10 @@
#include "dat.h"
#include "fns.h"
+Biobuf *stdin;
+Biobuf *stdout;
+Biobuf *stderr;
+
CPU ocpu, cpu;
Insn insn;
@@ -82,7 +86,7 @@
insn.pc = cpu.PC;
insn.op = decodeop(buf[0] = ifetch(&cpu));
if(insn.op == -1){
- fprint(2, "illegal opcode %#.2uhhx @ pc=%#.4uhx\n", buf[0], insn.pc);
+ Bprint(stderr, "illegal opcode %#.2uhhx @ pc=%#.4uhx\n", buf[0], insn.pc);
trap();
}
switch(ilen = insnlen(insn.op)){
@@ -126,18 +130,16 @@
prompt(void)
{
static char prev[256] = "";
- int n;
- char buf[256];
+ char *buf;
trapinit();
- if(interactive)
- print("8080> ");
- n = read(0, buf, sizeof(buf) - 1);
- if(n <= 0)
+ if(interactive){
+ Bprint(stdout, "8080> ");
+ Bflush(stdout);
+ }
+ buf = Brdstr(stdin, '\n', 1);
+ if(Blinelen(stdin) <= 0)
exits("eof");
- if(buf[n-1] != '\n')
- exits("nl");
- buf[n-1] = 0;
if(strcmp(buf, "") == 0){
if(strcmp(prev, "") == 0)
return;
@@ -148,7 +150,7 @@
}else if(strcmp(buf, "bpset") == 0){
}else if(strcmp(buf, "load") == 0){
if(loadrom("invaders.rom", 0) < 0)
- fprint(2, "load failed: %r\n");
+ Bprint(stderr, "load failed: %r\n");
}else if(strcmp(buf, "reg") == 0){
dumpregs();
}else if(strcmp(buf, "run") == 0){
@@ -156,8 +158,8 @@
cpu = ocpu;
else
for(;;){
- //print("%#.4uhx\t", cpu.PC);
- //das(mem+cpu.PC,nelem(mem));
+ Bprint(stdout, "%#.4uhx\t", cpu.PC);
+ das(mem+cpu.PC,nelem(mem));
cpustep();
}
}else if(strcmp(buf, "step") == 0){
@@ -173,7 +175,7 @@
}else if(strcmp(buf, "reset") == 0){
cpureset();
}else{
- fprint(2, "unknown command: %s\n", buf);
+ Bprint(stderr, "unknown command: %s\n", buf);
buf[0] = 0;
}
strcpy(prev, buf);
@@ -216,11 +218,12 @@
}ARGEND;
if(argc != 0)
usage();
+ stdin = Bfdopen(0, OREAD);
+ stdout = Bfdopen(1, OWRITE);
+ stderr = Bfdopen(2, OWRITE);
interactive = isatty();
fmtinstall('I', insnfmt);
cpureset();
- if(loadrom("invaders.rom", 0) < 0)
- fprint(2, "load failed: %r\n");
for(;;)
prompt();
}
--- a/das.c
+++ b/das.c
@@ -471,7 +471,7 @@
if((n = decode(&insn, mem, mlen)) < 0)
return -1;
- print("\t%I\n", &insn);
+ Bprint(stdout, "\t%I\n", &insn);
return n;
}
@@ -534,7 +534,7 @@
cpuexec(CPU *cpu, Insn *insn)
{
if(isa[insn->op].exec == nil){
- fprint(2, "%s (%#.2uhhx) not implemented!\n", opstr(insn->op), insn->op);
+ Bprint(stderr, "%s (%#.2uhhx) not implemented!\n", opstr(insn->op), insn->op);
trap();
}
itrace(opstr(insn->op));
--- a/dat.h
+++ b/dat.h
@@ -191,6 +191,10 @@
MAXTRACEOPS = 10,
};
+extern Biobuf *stdin;
+extern Biobuf *stdout;
+extern Biobuf *stderr;
+
extern CPU ocpu, cpu;
extern Insn insn;
extern int debug;
--- a/debug.c
+++ b/debug.c
@@ -1,5 +1,6 @@
#include <u.h>
#include <libc.h>
+#include <bio.h>
#include "dat.h"
#include "fns.h"
@@ -61,7 +62,7 @@
void
dumpinst(void)
{
- fprint(2, "op: %#.2x [%#.2x %#.2x]\n",
+ Bprint(stderr, "op: %#.2x [%#.2x %#.2x]\n",
mem[insn.pc+0], mem[insn.pc+1], mem[insn.pc+2]);
}
@@ -83,24 +84,24 @@
{
int i;
- fprint(2, "A=%#.2x\n", cpu.r[A]);
- fprint(2, "B=%#.2x\n", cpu.r[B]);
- fprint(2, "C=%#.2x\n", cpu.r[C]);
- fprint(2, "D=%#.2x\n", cpu.r[D]);
- fprint(2, "E=%#.2x\n", cpu.r[E]);
- fprint(2, "H=%#.2x\n", cpu.r[H]);
- fprint(2, "L=%#.2x\n", cpu.r[L]);
- fprint(2, "F=%#.2x", cpu.flg);
+ 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, "F=%#.2x", cpu.flg);
if(cpu.flg != 0){
- fprint(2, " (");
+ Bprint(stderr, " (");
for(i = 0; i < 8; i++)
if((cpu.flg&1<<i) != 0)
- fprint(2, "%c", flagchar(i));
- fprint(2, ")");
+ Bprint(stderr, "%c", flagchar(i));
+ Bprint(stderr, ")");
}
- fprint(2, "\n");
- fprint(2, "PC=%#.4x\n", cpu.PC);
- fprint(2, "SP=%#.4x\n", cpu.SP);
+ Bprint(stderr, "\n");
+ Bprint(stderr, "PC=%#.4x\n", cpu.PC);
+ Bprint(stderr, "SP=%#.4x\n", cpu.SP);
}
void
@@ -107,7 +108,7 @@
dumpmem(u16int s, u16int e)
{
while(s < e){
- fprint(2, "%.4x: %.2x\n", s, mem[s]);
+ Bprint(stderr, "%.4x: %.2x\n", s, mem[s]);
s++;
}
}
@@ -118,9 +119,9 @@
va_list args;
va_start(args, fmt);
- fprint(2, "%#.4x ", insn.pc);
- vfprint(2, fmt, args);
- fprint(2, "\n");
+ Bprint(stderr, "%#.4x ", insn.pc);
+ Bvprint(stderr, fmt, args);
+ Bprint(stderr, "\n");
va_end(args);
}
@@ -130,8 +131,8 @@
va_list args;
va_start(args, fmt);
- vfprint(2, fmt, args);
- fprint(2, "\n");
+ Bvprint(stderr, fmt, args);
+ Bprint(stderr, "\n");
va_end(args);
dumpinst();
dumpregs();
--- a/fns.h
+++ b/fns.h
@@ -6,7 +6,7 @@
char* rnam(u8int);
char* rpnam(u8int);
-#define dprint(...) if(debug)fprint(2, __VA_ARGS__)
+#define dprint(...) if(debug)Bprint(stderr, __VA_ARGS__)
#define itrace(...) if(tracing>0)itrace0(__VA_ARGS__)
/* disassembler */
--- a/mem.c
+++ b/mem.c
@@ -1,5 +1,6 @@
#include <u.h>
#include <libc.h>
+#include <bio.h>
#include "dat.h"
#include "fns.h"
@@ -7,7 +8,7 @@
memread(u16int a)
{
if(a >= MEMSZ){
- fprint(2, "memread failed addr=%#.4x op=%#0.2x pc=%#.4x\n", a, insn.op, ocpu.PC);
+ Bprint(stderr, "memread failed addr=%#.4x op=%#0.2x pc=%#.4x\n", a, insn.op, ocpu.PC);
trap();
}
return mem[a];
@@ -17,7 +18,7 @@
memwrite(u16int a, u8int x)
{
if(a < ROMSZ || a >= MEMSZ){
- fprint(2, "write failed addr=%#.4x op=%#0.2x pc=%#.4x\n", a, insn.op, ocpu.PC);
+ Bprint(stderr, "write failed addr=%#.4x op=%#0.2x pc=%#.4x\n", a, insn.op, ocpu.PC);
trap();
}
mem[a] = x;
@@ -27,7 +28,7 @@
ifetch(CPU *cpu)
{
if(cpu->PC >= ROMSZ){
- fprint(2, "ifetch failed pc=%#.4x\n", cpu->PC);
+ Bprint(stderr, "ifetch failed pc=%#.4x\n", cpu->PC);
trap();
}
return memread(cpu->PC++);