/* Copyright (c) 1996-1997 Swiss Federal Institute of Technology, Computer Engineering and Networks Laboratory. All rights reserved. Written by George Fankhauser . For more documentation please visit http://www.tik.ee.ethz.ch/~gfa. This device is root counter 2 of the PSX File: $Source: /usr/drwho/vault/cvs/topsy/MipsSim/PSXClock.java,v $ Author(s): G. Fankhauser Affiliation: ETH Zuerich, TIK Version: $Revision: 1.2 $ Creation Date: December 1999 Last Date of Change: $Date: 2000/05/08 19:08:27 $ by: $Author: gfa $ $Log: PSXClock.java,v $ Revision 1.2 2000/05/08 19:08:27 gfa *** empty log message *** */ import MemoryRegion; import ClockErrorException; public class PSXClock extends MemoryRegion { final int CLOCKADDRSIZE = 12; final int COUNTER0 = 0x00; final int COUNTER1 = 0x04; final int COUNTER2 = 0x08; static final int CLOCKSPEED = 1; // if the clock were real // this would be one // ctrl register: sc1 sc0 rw1 rw0 m2 m1 m0 bcd byte mode = 2; int counter = 0; int counterInitValue = 0; byte control; boolean lsb = true; public PSXClock(int baseAddress) { from = baseAddress; to = baseAddress+CLOCKADDRSIZE; } // read/write depends on hi/low byte settings of the control register byte readByte(int address) throws ClockErrorException { // must be allowed for use with IDT board :-( // throw new ClockErrorException(); return 0; } void writeByte(int address, byte data) throws ClockErrorException { int addr = address - from; byte mode = (byte)(0); if ((addr == 0) || (addr == 0) || (addr == 0)) { writeCountRegister(data); } else if (addr == 0) { if ((data & mode) == mode) { control = data; lsb = true; } else { throw new ClockErrorException("mode not supported"); } } else { throw new ClockErrorException("invalid address"); } } void writeCountRegister(byte data) { if (lsb) { counterInitValue &= 0x0000ff00; counterInitValue |= data; lsb = false; } else { int d = data << 8; d &= 0x0000ff00; counterInitValue &= 0x000000ff; counterInitValue |= d; counter = counterInitValue; } } // System Clock is 33.8688 MHz, timer clock 4.2336 // to get 100 Hz we divide by 42336 void checkInterrupt(Processor p) { counter -= CLOCKSPEED; if (counter <= 1) { //System.out.println("clock hit"); counter = counterInitValue; p.interrupt(0); // post interrupt #0 } } }