|
|
|
@ -17,23 +17,11 @@ import java.util.Timer;
|
|
|
|
|
import java.util.TimerTask;
|
|
|
|
|
import java.util.concurrent.Semaphore;
|
|
|
|
|
|
|
|
|
|
public class CpuEntity extends Peripheral implements Runnable{
|
|
|
|
|
public class CpuEntity extends Peripheral{
|
|
|
|
|
public int i = 0;
|
|
|
|
|
|
|
|
|
|
static long interruptTimer = 50L;
|
|
|
|
|
//Adjust as needed to match original clock speed
|
|
|
|
|
|
|
|
|
|
TimerTask cpuTask = new TimerTask(){
|
|
|
|
|
public void run(){
|
|
|
|
|
for(int i = 0; i < 50; i++)
|
|
|
|
|
step();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static Timer timer = new Timer();
|
|
|
|
|
boolean notTicked = true;
|
|
|
|
|
private boolean running = false;
|
|
|
|
|
private Semaphore waiInterrupt = new Semaphore(2);
|
|
|
|
|
private RCCpu cpu;
|
|
|
|
|
public RCCpu cpu;
|
|
|
|
|
private RCBus rcbus;
|
|
|
|
|
private Path bootloader;
|
|
|
|
|
|
|
|
|
@ -54,7 +42,7 @@ public class CpuEntity extends Peripheral implements Runnable{
|
|
|
|
|
be.cpu = new RCCpu(world, pos);
|
|
|
|
|
be.setRCBus(new RCBus(new RedBus()));
|
|
|
|
|
be.cpu.setBus(be.getRCBus());
|
|
|
|
|
be.cpu.setBus(new Bus(be.getRCBus().getRedBus()));
|
|
|
|
|
be.cpu.setBus(be.getRCBus());
|
|
|
|
|
Memory ram = new Memory(0x0000, 0x2000);
|
|
|
|
|
try {
|
|
|
|
|
ram.loadFromFile(be.bootloader, 0x400, 0x100);
|
|
|
|
@ -64,12 +52,12 @@ public class CpuEntity extends Peripheral implements Runnable{
|
|
|
|
|
be.cpu.getBus().addDevice(ram);
|
|
|
|
|
be.reset();
|
|
|
|
|
be.notTicked = false;
|
|
|
|
|
be.start();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void start(){
|
|
|
|
|
timer.schedule(cpuTask, new Date(), 2L);
|
|
|
|
|
for(int i = 0; i < 50; i++)
|
|
|
|
|
be.step();
|
|
|
|
|
if(be.cpu.isWaitingForInterrupt()){
|
|
|
|
|
be.cpu.setInterrupt(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -102,47 +90,19 @@ public class CpuEntity extends Peripheral implements Runnable{
|
|
|
|
|
this.defaultMonitorId = id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean isRunning() {
|
|
|
|
|
return this.running;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void run() {
|
|
|
|
|
this.running = true;
|
|
|
|
|
do {
|
|
|
|
|
step();
|
|
|
|
|
} while (this.running);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void stop() {
|
|
|
|
|
this.running = false;
|
|
|
|
|
while (this.waiInterrupt.availablePermits() <= 0) {
|
|
|
|
|
this.waiInterrupt.release();
|
|
|
|
|
}
|
|
|
|
|
this.waiInterrupt.drainPermits();
|
|
|
|
|
this.waiInterrupt.release(2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void reset() {
|
|
|
|
|
stop();
|
|
|
|
|
this.cpu.reset();
|
|
|
|
|
this.getRCBus().write(0, this.defaultDriveId, getWorld(), this.getPos());
|
|
|
|
|
this.getRCBus().write(1, this.defaultMonitorId, getWorld(), this.getPos());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void step() {
|
|
|
|
|
this.waiInterrupt.acquireUninterruptibly();
|
|
|
|
|
this.cpu.step();
|
|
|
|
|
this.getRCBus().update();
|
|
|
|
|
if (this.cpu.isStopped()) {
|
|
|
|
|
this.stop();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (this.cpu.isWaitingForInterrupt()) {
|
|
|
|
|
this.waiInterrupt.acquireUninterruptibly();
|
|
|
|
|
this.cpu.assertIrq();
|
|
|
|
|
}
|
|
|
|
|
if (this.waiInterrupt.availablePermits() < 2) {
|
|
|
|
|
this.waiInterrupt.release();
|
|
|
|
|
i++;
|
|
|
|
|
if(!this.cpu.isWaitingForInterrupt()) {
|
|
|
|
|
this.cpu.step();
|
|
|
|
|
this.getBus().update();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|