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; ...@@ -26,9 +26,9 @@ import me.felinewith.kcpu.util.HardwareMemIrq;
*/ */
public class KBoard extends BaseDevice implements IMemory { public class KBoard extends BaseDevice implements IMemory {
private final ThreadPoolExecutor tpe; final ThreadPoolExecutor tpe;
private TimerTask tt; TimerTask tt;
private Timer t; Timer t;
private final IDevice[] devices; private final IDevice[] devices;
private final KMemoryRange[] deviceMaps; private final KMemoryRange[] deviceMaps;
...@@ -115,7 +115,7 @@ public class KBoard extends BaseDevice implements IMemory { ...@@ -115,7 +115,7 @@ public class KBoard extends BaseDevice implements IMemory {
short devAddr = (short)(addr - devOff); short devAddr = (short)(addr - devOff);
return memDevs[rdev].read(devAddr); return memDevs[rdev].read(devAddr);
} }
return memory[addr]; return memory[0xffff & addr];
} }
@Override public void write(short addr, byte data) { @Override public void write(short addr, byte data) {
...@@ -130,9 +130,13 @@ public class KBoard extends BaseDevice implements IMemory { ...@@ -130,9 +130,13 @@ public class KBoard extends BaseDevice implements IMemory {
short devOff = memDevMaps[rdev].getOffset(); short devOff = memDevMaps[rdev].getOffset();
short devAddr = (short)(addr - devOff); short devAddr = (short)(addr - devOff);
memDevs[rdev].write(devAddr, data); 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) { public void attachDevice(int port, int offset, IDevice device) {
...@@ -186,6 +190,39 @@ public class KBoard extends BaseDevice implements IMemory { ...@@ -186,6 +190,39 @@ public class KBoard extends BaseDevice implements IMemory {
return 0x7f; 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() { @Override public void exec() {
try { try {
if ( bufin.ready() ) { if ( bufin.ready() ) {
......
...@@ -40,7 +40,7 @@ public class Kcpu extends TimerTask implements IDevice { ...@@ -40,7 +40,7 @@ public class Kcpu extends TimerTask implements IDevice {
@Override public byte read(short addr) { @Override public byte read(short addr) {
if ( (0xffff & addr) > length() ) { return (byte)0xff; } if ( (0xffff & addr) > length() ) { return (byte)0xff; }
return memory[addr]; return memory[0xffff & addr];
} }
@Override public void write(short offset, byte data) { } @Override public void write(short offset, byte data) { }
...@@ -56,6 +56,7 @@ public class Kcpu extends TimerTask implements IDevice { ...@@ -56,6 +56,7 @@ public class Kcpu extends TimerTask implements IDevice {
@Override public void sendIrq(byte irq) { board.sendIrq(irq); } @Override public void sendIrq(byte irq) { board.sendIrq(irq); }
private void workCycle() { private void workCycle() {
if ( pc >= 0xfff0 ) { pc = 0; }
short inst = MemHelper.readShort(pc, board); short inst = MemHelper.readShort(pc, board);
short ref = MemHelper.readShort(pc + 2, board); short ref = MemHelper.readShort(pc + 2, board);
short[] values = new short[4]; short[] values = new short[4];
...@@ -72,45 +73,43 @@ public class Kcpu extends TimerTask implements IDevice { ...@@ -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() ) { if ( od.hasHandler() ) {
registers = od.call(pc, ref, registers, values, board); registers = od.call(pc, ref, registers, values, board);
int vt = od.getArgs(); pc += 12;
pc += 2;
if ( vt != 0 ) { pc += 2 + (vt * 2); }
return; return;
} }
pc += 12;
short data; short data;
data = ValueHelper.readValue(0, ref, registers, values, board); data = ValueHelper.readValue(0, ref, registers, values, board);
switch (od) { switch (od) {
// ask what device is using which address // device management
case ASK: case DEVA: registers[0] = board.checkDevice(data); return;
data = board.checkDevice((short)(0x000f & data)); case DEVD: board.detachDevice(data); return;
registers = ValueHelper.writeValue(1, ref, registers, values, board, data); case DEVM:
short d2 = ValueHelper.readValue(1, ref, registers, values, board);
board.setDeviceRange(data, d2);
return; return;
case DEVP:
// interrupt to port KMemoryRange kmr = board.getDeviceRange(data);
case IRQD: registers[0] = kmr.getOffset();
board.sendIrq((byte)(0x000f & data)); registers[1] = kmr.getLength();
return; return;
// interrupt to memory address // interrupts
case IRQM: case IRQD: board.sendIrq((byte)(0x000f & data)); return;
board.memIrq(data); case IRQM: board.memIrq(data); return;
return;
// jump absolute // jumps
case JMP: pc = (short)(pc + (0xffff & data)); return;
case JMPA: pc = data; return; case JMPA: pc = data; return;
// jump down
case JMPD: pc = (short)(pc - (0xffff & data)); return; case JMPD: pc = (short)(pc - (0xffff & data)); return;
// jump up case ZPWR:
case JMP: pc = (short)(pc + (0xffff & data)); return; board.t.cancel();
} }
} }
} }
...@@ -11,7 +11,15 @@ public class BootRom extends BaseMemory { ...@@ -11,7 +11,15 @@ public class BootRom extends BaseMemory {
public BootRom() { public BootRom() {
memory = new byte[0x1000]; 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) { } @Override public void write(short offset, byte data) { }
......
...@@ -15,17 +15,14 @@ public class BasicMath implements IHandler { ...@@ -15,17 +15,14 @@ public class BasicMath implements IHandler {
short vb = ValueHelper.readValue(1, ref, output, values, memory); short vb = ValueHelper.readValue(1, ref, output, values, memory);
switch (opcode) { switch (opcode) {
case ADD: va += vb; break; case ADD: output[0] = (short)(va + vb); break;
case DIV: case DIV:
short rem = (short)(va % vb); output[0] = (short)(va / vb);
va /= vb; output[1] = (short)(va % vb);
vb = rem;
break; break;
case MUL: va *= vb; break; case MUL: output[0] = (short)(va * vb); break;
case SUB: 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; return output;
} }
......
...@@ -18,14 +18,19 @@ public enum OpDirector { ...@@ -18,14 +18,19 @@ public enum OpDirector {
MOV(0x0011, Memory.class), MOV(0x0011, Memory.class),
// cpu stuffies // 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(0xff10, null), // interrupt to port
IRQD (0x8f10, null), IRQM(0xff11, null), // interrupt to memory
JMP (0x8f08, null), JMP (0xff18, null), // jump up
JMPA (0x8f0c, null), JMPA(0xff19, null), // jump absolute addr
JMPD (0x8f18, null), JMPD(0xff1a, null), // jump down
ZPWR(0xffff, null), // power off
; ;
...@@ -51,14 +56,4 @@ public enum OpDirector { ...@@ -51,14 +56,4 @@ public enum OpDirector {
catch (IllegalAccessException | InstantiationException e) {} catch (IllegalAccessException | InstantiationException e) {}
return regs; 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 { ...@@ -17,13 +17,13 @@ public abstract class BaseMemory implements IMemory {
public final byte read(int addr) { return read((short)addr); } public final byte read(int addr) { return read((short)addr); }
@Override public byte read(short addr) { @Override public byte read(short addr) {
if ( (0xffff & addr) > length() ) { return (byte)0xff; } 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); } public final void write(int addr, byte data) { write((short)addr, data); }
@Override public void write(short addr, byte data) { @Override public void write(short addr, byte data) {
if ( (0xffff & addr) > length() ) { return; } 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); } 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!