Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private final int ram_size_in_bytes = 0x4000;
- private final int kbd_buf_head_addr = 0x7000;
- private final int kbd_buf_tail_addr = 0x7002;
- private final int kbd_buf_out_addr = 0x7004;
- private final int wai_addr = 0x7008;
- private final int utime_addr = 0x700A;
- private final int bus_output_addr = 0x7500;
- private final int bus_input_addr = 0x7600;
- // Video RAM.
- public byte[] video_ram = new byte[10240];
- // Main RAM.
- public short[] ram = new short[ram_size_in_bytes / 2];
- // Bus input window.
- public short[] cpu_bus_window = new short[128];
- // Ring buffer keyboard.
- public byte[] keyboard_buf = new byte[16];
- public byte keyboard_buf_head = 0;
- public byte keyboard_buf_tail = 0;
- // Address input and output windows on bus.
- public int peripheral_id = 1;
- public int cpu_id = 0;
- // CPU.
- // Program counter.
- public short pc = 0;
- // Return stack.
- public short[] rstack = new short[32];
- public byte rstack_pointer = 0;
- // Data stack.
- public short[] dstack = new short[32];
- public short dstack_T = 0;
- public short dstack_N = 0;
- public byte dstack_depth = 0;
- private final short forth_true = (short)0xFFFF;
- private final short forth_false = 0;
- private boolean wai_flag = false;
- public boolean isTurnedOn = false;
- private String inventoryName = "CPU";
- public ItemStack[] inventory;
- private boolean busTimeout = false;
- private INedoPeripheral peripheralCache = null;
- private int conMask = 0;
- public TileEntityCPU() {
- for (int i = 0; i < video_ram.length; i++) video_ram[i] = ' ';
- video_ram[0x2600] = 0; video_ram[0x2602] = 0; video_ram[0x2604] = 0;
- inventory = new ItemStack[getSizeInventory()];
- }
- private void write_mem(int addr, short data) {
- if (addr < ram_size_in_bytes) {
- ram[addr >>> 1] = data;
- } else if ((addr >= ram_size_in_bytes) && (addr < ram_size_in_bytes + video_ram.length)) {
- video_ram[addr - ram_size_in_bytes] = (byte)data;
- } else if ((addr & 0xFFFE) == kbd_buf_head_addr) {
- keyboard_buf_head = (byte)(data & 0x0F);
- } else if ((addr & 0xFFFE) == wai_addr) {
- wai_flag = true;
- } else if ((addr & 0xFFFE) == 0x7012) {
- // Map device to bus window.
- if (peripheral_id != data) {
- if (peripheralCache != null) busTimeout = true;
- peripheral_id = data & 0xFF;
- }
- } else if ((addr >= bus_output_addr) && (addr <= bus_output_addr + 0xFF) && (cpu_id != peripheral_id)) {
- if (peripheralCache == null) {
- peripheralCache = NedoBusUtils.searchPeripheralBlock(worldObj,
- new ChunkCoordinates(xCoord, yCoord, zCoord), conMask, peripheral_id);
- }
- if (peripheralCache == null) {
- busTimeout = true;
- } else {
- peripheralCache.busWrite(addr - bus_output_addr, data);
- }
- } else if ((addr >= bus_input_addr) && (addr <= bus_input_addr + 0xFF)) {
- cpu_bus_window[(addr - bus_input_addr) >>> 1] = data;
- }
- }
- private short read_mem(int addr) {
- if (addr < ram_size_in_bytes) return ram[addr >>> 1];
- if ((addr & 0xFFFE) == kbd_buf_head_addr) return keyboard_buf_head;
- if ((addr & 0xFFFE) == kbd_buf_tail_addr) return keyboard_buf_tail;
- if ((addr & 0xFFFE) == kbd_buf_out_addr) return keyboard_buf[keyboard_buf_head];
- if ((addr >= bus_output_addr) && (addr <= bus_output_addr + 0xFF) && (cpu_id != peripheral_id)) {
- if (peripheralCache == null) {
- peripheralCache = NedoBusUtils.searchPeripheralBlock(worldObj,
- new ChunkCoordinates(xCoord, yCoord, zCoord), conMask, peripheral_id);
- }
- if (peripheralCache == null) {
- busTimeout = true;
- return 0;
- } else {
- return peripheralCache.busRead(addr - bus_output_addr);
- }
- } else if ((addr >= bus_input_addr) && (addr <= bus_input_addr + 0xFF)) {
- return cpu_bus_window[(addr - bus_input_addr) >>> 1];
- }
- if ((addr & 0xFFFE) == utime_addr) return (short)((System.currentTimeMillis() / 1000L) & 0xFFFF);
- if ((addr & 0xFFFE) == utime_addr + 2) return (short)((System.currentTimeMillis() / 1000L) >>> 16 );
- return 0;
- }
- private short ALU(int op) {
- switch (op) {
- case 0: return dstack_T;
- case 1: return dstack_N;
- case 2: return (short)(dstack_T + dstack_N);
- case 3: return (short)(dstack_T & dstack_N);
- case 4: return (short)(dstack_T | dstack_N);
- case 5: return (short)((dstack_T << 8) | ((dstack_T >> 8) & 0xFF));
- case 6: return (short)~dstack_T;
- case 7: return (dstack_N == dstack_T) ? forth_true : forth_false;
- case 8: return (dstack_N < dstack_T) ? forth_true : forth_false;
- case 9: return (short)((dstack_T >> 1) & 0x7FFF);
- case 10: return (short)(dstack_T - 1);
- case 11: return rstack[rstack_pointer];
- case 12: return read_mem(((int)dstack_T) & 0xFFFF);
- case 13: return (short)(dstack_T << 1);
- case 14: return dstack_depth;
- case 15: return (((int)dstack_N & 0xFFFF) < ((int)dstack_T & 0xFFFF)) ? forth_true : forth_false;
- default: return 0;
- }
- }
- private void execute_insn() {
- short dstack_T_buf = dstack_T;
- short istr = read_mem(pc << 1);
- if ((istr & 0x8000) != 0) {
- // literal.
- dstack_depth = (byte)((dstack_depth + 1) & 0x1F);
- dstack[dstack_depth] = dstack_N;
- dstack_N = dstack_T;
- dstack_T = (short)(istr & 0x7FFF);
- pc = (short)((pc + 1) & 0x1FFF);
- } else if ((istr & 0xE000) == 0) {
- // jump.
- pc = (short)(istr & 0x1FFF);
- } else if ((istr & 0xE000) == 0x2000) {
- // conditional jump.
- if (dstack_T != 0) {
- pc = (short)((pc + 1) & 0x1FFF);
- } else {
- pc = (short)(istr & 0x1FFF);
- }
- dstack_T = dstack_N;
- dstack_N = dstack[dstack_depth];
- dstack_depth = (byte)((dstack_depth - 1) & 0x1F);
- } else if ((istr & 0xE000) == 0x4000) {
- // call.
- rstack_pointer = (byte)((rstack_pointer + 1) & 0x1F);
- rstack[rstack_pointer] = (short)(((pc + 1) & 0x1FFF) << 1);
- pc = (short)(istr & 0x1FFF);
- } else if ((istr & 0xE000) == 0x6000) {
- // ALU.
- // N -> [T]
- if ((istr & (1 << 5)) != 0 ) {
- write_mem(((int)dstack_T) & 0xFFFF, dstack_N);
- }
- // dstack+-
- if ((istr & 3) == 0) {
- dstack_T = ALU((istr >> 8) & 0x0F);
- if ((istr & (1 << 7)) != 0 ) dstack_N = dstack_T_buf; // T -> N
- } else if ((istr & 3) == 1) {
- dstack_T = ALU((istr >> 8) & 0x0F);
- dstack_depth = (byte)((dstack_depth + 1) & 0x1F);
- dstack[dstack_depth] = dstack_N;
- if ((istr & (1 << 7)) != 0 ) dstack_N = dstack_T_buf; // T -> N
- } else if ((istr & 3) == 2) {
- // Error.
- } else if ((istr & 3) == 3) {
- dstack_T = ALU((istr >> 8) & 0x0F);
- dstack_N = dstack[dstack_depth];
- dstack_depth = (byte)((dstack_depth - 1) & 0x1F);
- }
- // R -> PC
- if ((istr & (1 << 12)) != 0 ) {
- pc = (short)(rstack[rstack_pointer] >> 1);
- } else {
- pc = (short)((pc + 1) & 0x1FFF);
- }
- // rstack+-
- if (((istr >> 2) & 3) == 1) {
- rstack_pointer = (byte)((rstack_pointer + 1) & 0x1F);
- rstack[rstack_pointer] = dstack_T_buf;
- } else if (((istr >> 2) & 3) == 3) {
- rstack_pointer = (byte)((rstack_pointer - 1) & 0x1F);
- }
- }
- }
- public void turnOn() throws IOException {
- if (inventory[0] == null) return;
- if (!(inventory[0].getItem() instanceof ItemEEPROM ||
- inventory[0].getItem() instanceof ItemForthROM)) return;
- for (int i = 0; i < video_ram.length; i++) video_ram[i] = ' ';
- video_ram[0x2600] = 0; video_ram[0x2602] = 0; video_ram[0x2604] = 0;
- keyboard_buf_head = 0; keyboard_buf_tail = 0;
- for (int i = 0; i < keyboard_buf.length; i++) keyboard_buf[i] = 0;
- pc = 0;
- rstack_pointer = 0;
- for (int i = 0; i < rstack.length; i++) rstack[i] = 0;
- dstack_depth = 0; dstack_N = 0; dstack_N = 0;
- for (int i = 0; i < dstack.length; i++) dstack[i] = 0;
- if (inventory[0].getItem() instanceof ItemForthROM) {
- InputStream input =
- getClass().getResourceAsStream("/assets/nedocomputers/forth/forth.bin");
- DataInputStream d_stream = new DataInputStream(input);
- try {
- for (int i = 0; i < ram.length; i++) {
- byte b1 = d_stream.readByte();
- byte b2 = d_stream.readByte();
- ram[i] = (short)(((int)b1 & 0xFF) + ((int)b2 & 0xFF) * 256);
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- isTurnedOn = true;
- } else if (inventory[0].getItem() instanceof ItemEEPROM) {
- if (inventory[0].hasTagCompound()) {
- NBTTagCompound nbtTag = inventory[0].getTagCompound();
- if (nbtTag.hasKey("id")) {
- String id = nbtTag.getString("id");
- List files = new ArrayList();
- File dir = new File(DimensionManager.getCurrentSaveRootDirectory(), Settings.EEPROM_DIR);
- if (!dir.exists() || !dir.isDirectory())
- dir.mkdirs();
- for (File f : dir.listFiles())
- if (f.isFile())
- files.add(f.getName());
- if (files.contains(id.concat(".bin"))) {
- byte[] ram_buf = new byte[ram.length * 2];
- FileInputStream fs = new FileInputStream(new File(dir, id.concat(".bin")));
- fs.read(ram_buf, 0, ram_size_in_bytes);
- fs.close();
- ByteBuffer.wrap(ram_buf).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer().get(ram);
- isTurnedOn = true;
- }
- } else if (nbtTag.hasKey("ram")) {
- byte[] ram_buf = nbtTag.getByteArray("ram");
- ByteBuffer.wrap(ram_buf).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer().get(ram);
- isTurnedOn = true;
- }
- }
- }
- }
- public void turnOff() {
- for (int i = 0; i < video_ram.length; i++) video_ram[i] = ' ';
- video_ram[0x2600] = 0; video_ram[0x2602] = 0; video_ram[0x2604] = 0;
- this.isTurnedOn = false;
- }
- public void reset() {
- for (int i = 0; i < video_ram.length; i++) video_ram[i] = ' ';
- video_ram[0x2600] = 0; video_ram[0x2602] = 0; video_ram[0x2604] = 0;
- pc = 0;
- rstack_pointer = 0;
- for (int i = 0; i < rstack.length; i++) rstack[i] = 0;
- dstack_depth = 0; dstack_N = 0; dstack_N = 0;
- for (int i = 0; i < dstack.length; i++) dstack[i] = 0;
- }
- @Override
- public void updateEntity() {
- int cnt = Settings.INSTRUCTIONS_PER_TICK;
- wai_flag = false;
- busTimeout = false;
- peripheralCache = null;
- while ((cnt > 0) && !wai_flag && !busTimeout && isTurnedOn) {
- execute_insn();
- cnt--;
- }
- }
- public void KeyTyped(byte chr) {
- if (isTurnedOn) {
- if ((chr != 0xFF) && (((keyboard_buf_tail + 1) % keyboard_buf.length) != keyboard_buf_head)) {
- keyboard_buf_tail = (byte)((keyboard_buf_tail + 1) % keyboard_buf.length);
- keyboard_buf[keyboard_buf_tail] = chr;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement