Commit eb9f4a4c by Jessica Hawkwell

Added new ops, cleanup

1 parent 0ce4a166
Pipeline #207 passed
in 1 minute 4 seconds
......@@ -26,9 +26,9 @@ import me.felinewith.kcpu.util.HardwareMemIrq;
*/
public class KBoard extends BaseDevice implements IMemory {
private final ThreadPoolExecutor tpe;
private TimerTask tt;
private Timer t;
final ThreadPoolExecutor tpe;
TimerTask tt;
Timer t;
private final IDevice[] devices;
private final KMemoryRange[] deviceMaps;
......@@ -115,7 +115,7 @@ public class KBoard extends BaseDevice implements IMemory {
short devAddr = (short)(addr - devOff);
return memDevs[rdev].read(devAddr);
}
return memory[addr];
return memory[0xffff & addr];
}
@Override public void write(short addr, byte data) {
......@@ -130,9 +130,13 @@ public class KBoard extends BaseDevice implements IMemory {
short devOff = memDevMaps[rdev].getOffset();
short devAddr = (short)(addr - devOff);
memDevs[rdev].write(devAddr, data);
if (rdev == 0) {
try { bufout.write(0xff & data); }
catch (IOException x) {}
}
}
memory[addr] = data;
memory[0xffff & addr] = data;
}
public void attachDevice(int port, int offset, IDevice device) {
......@@ -186,6 +190,39 @@ public class KBoard extends BaseDevice implements IMemory {
return 0x7f;
}
public void detachDevice(short port) {
if ( port >= 0x20 ) { return; }
if ( port <= 0x0f ) { deviceMaps[port] = null; }
else { memDevMaps[(short)(0x0f & port)] = null; }
}
public KMemoryRange getDeviceRange(short port) {
if ( port >= 0x20 ) { return new KMemoryRange((short)0, (short)0); }
KMemoryRange kmr;
if ( port <= 0x0f ) { kmr = deviceMaps[port]; }
else { kmr = memDevMaps[(short)(0x0f & port)]; }
return new KMemoryRange(kmr.getOffset(), kmr.getLength());
}
public void setDeviceRange(short device, short addr) {
if ( device >= 0x20 ) { return; }
short dev = (short)(0x0f & device);
if ( device <= 0x0f ) {
if ( devices[dev] == null ) { }
else if ( deviceMaps[dev] == null ) {
deviceMaps[dev] = new KMemoryRange(addr, devices[dev].length());
}
else { deviceMaps[dev].offset = addr; }
}
else {
if ( memDevs[dev] == null ) { }
else if ( memDevMaps[dev] == null ) {
memDevMaps[dev] = new KMemoryRange(addr, memDevs[dev].length());
}
else { memDevMaps[dev].offset = addr; }
}
}
@Override public void exec() {
try {
if ( bufin.ready() ) {
......
......@@ -40,7 +40,7 @@ public class Kcpu extends TimerTask implements IDevice {
@Override public byte read(short addr) {
if ( (0xffff & addr) > length() ) { return (byte)0xff; }
return memory[addr];
return memory[0xffff & addr];
}
@Override public void write(short offset, byte data) { }
......@@ -56,6 +56,7 @@ public class Kcpu extends TimerTask implements IDevice {
@Override public void sendIrq(byte irq) { board.sendIrq(irq); }
private void workCycle() {
if ( pc >= 0xfff0 ) { pc = 0; }
short inst = MemHelper.readShort(pc, board);
short ref = MemHelper.readShort(pc + 2, board);
short[] values = new short[4];
......@@ -72,45 +73,43 @@ public class Kcpu extends TimerTask implements IDevice {
}
}
if ( od == null ) { pc += 2; return; } // inc 2 if nop
if ( od == null ) { pc += 12; return; } // inc 2 if nop
if ( od.hasHandler() ) {
registers = od.call(pc, ref, registers, values, board);
int vt = od.getArgs();
pc += 2;
if ( vt != 0 ) { pc += 2 + (vt * 2); }
pc += 12;
return;
}
pc += 12;
short data;
data = ValueHelper.readValue(0, ref, registers, values, board);
switch (od) {
// ask what device is using which address
case ASK:
data = board.checkDevice((short)(0x000f & data));
registers = ValueHelper.writeValue(1, ref, registers, values, board, data);
// device management
case DEVA: registers[0] = board.checkDevice(data); return;
case DEVD: board.detachDevice(data); return;
case DEVM:
short d2 = ValueHelper.readValue(1, ref, registers, values, board);
board.setDeviceRange(data, d2);
return;
// interrupt to port
case IRQD:
board.sendIrq((byte)(0x000f & data));
case DEVP:
KMemoryRange kmr = board.getDeviceRange(data);
registers[0] = kmr.getOffset();
registers[1] = kmr.getLength();
return;
// interrupt to memory address
case IRQM:
board.memIrq(data);
return;
// interrupts
case IRQD: board.sendIrq((byte)(0x000f & data)); return;
case IRQM: board.memIrq(data); return;
// jump absolute
// jumps
case JMP: pc = (short)(pc + (0xffff & data)); return;
case JMPA: pc = data; return;
// jump down
case JMPD: pc = (short)(pc - (0xffff & data)); return;
// jump up
case JMP: pc = (short)(pc + (0xffff & data)); return;
case ZPWR:
board.t.cancel();
}
}
}
......@@ -11,7 +11,15 @@ public class BootRom extends BaseMemory {
public BootRom() {
memory = new byte[0x1000];
Arrays.fill(memory, (byte)0x75);
Arrays.fill(memory, (byte)0xff);
// hardcoding some bootrom
memory[0] = 0x11;
memory[1] = 0x00;
memory[2] = 0x01;
memory[3] = 0x00;
memory[4] = 0x61;
memory[5] = 0x41;
}
@Override public void write(short offset, byte data) { }
......
......@@ -15,17 +15,14 @@ public class BasicMath implements IHandler {
short vb = ValueHelper.readValue(1, ref, output, values, memory);
switch (opcode) {
case ADD: va += vb; break;
case ADD: output[0] = (short)(va + vb); break;
case DIV:
short rem = (short)(va % vb);
va /= vb;
vb = rem;
output[0] = (short)(va / vb);
output[1] = (short)(va % vb);
break;
case MUL: va *= vb; break;
case SUB: va -= vb; break;
case MUL: output[0] = (short)(va * vb); break;
case SUB: output[0] = (short)(va - vb); break;
}
output = ValueHelper.writeValue(0, ref, output, values, memory, va);
output = ValueHelper.writeValue(1, ref, output, values, memory, vb);
return output;
}
......
......@@ -18,14 +18,19 @@ public enum OpDirector {
MOV(0x0011, Memory.class),
// cpu stuffies
ASK(0x1000, null),
DEVA(0xff00, null), // get device port from address
DEVD(0xff01, null), // detach device
DEVM(0xff02, null), // relocate device
DEVP(0x1003, null), // get device address from port
IRQM (0x8f00, null),
IRQD (0x8f10, null),
IRQD(0xff10, null), // interrupt to port
IRQM(0xff11, null), // interrupt to memory
JMP (0x8f08, null),
JMPA (0x8f0c, null),
JMPD (0x8f18, null),
JMP (0xff18, null), // jump up
JMPA(0xff19, null), // jump absolute addr
JMPD(0xff1a, null), // jump down
ZPWR(0xffff, null), // power off
;
......@@ -51,14 +56,4 @@ public enum OpDirector {
catch (IllegalAccessException | InstantiationException e) {}
return regs;
}
public int getArgs() {
try {
if (c == null) { return 0; }
IHandler ih = c.newInstance();
return ih.getArgs(this);
}
catch (IllegalAccessException | InstantiationException e) {}
return 0;
}
}
......@@ -17,13 +17,13 @@ public abstract class BaseMemory implements IMemory {
public final byte read(int addr) { return read((short)addr); }
@Override public byte read(short addr) {
if ( (0xffff & addr) > length() ) { return (byte)0xff; }
return memory[addr];
return memory[0xffff & addr];
}
public final void write(int addr, byte data) { write((short)addr, data); }
@Override public void write(short addr, byte data) {
if ( (0xffff & addr) > length() ) { return; }
memory[addr] = data;
memory[0xffff & addr] = data;
}
public final void copy(int addrSrc, int addrDest, IMemory dest) { copy((short)addrSrc, (short)addrDest, dest); }
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!