Package CPU

Class MC68000Cpu

java.lang.Object
CPU.MC68000Cpu
Direct Known Subclasses:
CPUforScreenEmulation

public class MC68000Cpu extends Object
This is the "cpu" used. Some implementation notes.
  • It has a provision for "external interrupt" handlers : before an smsqmulator2.cpu.Instruction is executed, a check is made whether an external interrupt handler caused an interrupt. The CPU has a list of external interrupt handlers which are registered with it.
  • This CPU registers a few more "instructions" (from JavaComm) which are normally illegal (A line) instructions but are used to communicate with the TrapDispatcher.
  • It acts on the trace bit by calling the corresponding exception routine.
  • For performance considerations, the object exposes the address, data and status registers and PC to the outside world as public arrays of int. This is so that the instructions can manipulate the registers directly.
  • The memory is directly incorporated into this object.
  • This object implements memory as an array of shorts. Profiling of SMSQ/E and typical applications has shown that word sized access is by far the most frequent access to memory. YMMV. As a result, the PC is an index into the memory, not the "true" address. To get the true address, multiply the PC by 2.
  • The memory read and write operations for words and long word DO NOT CHECK that access is to an even address as it should be. If it isn't, then the read/write operation will take place at address-1!!!!!!!!!!!!!!!=)
  • It implements a "memory cutoff". Indeed, and unfortunately, QLiberator compiled programs use the MS bits of some addresses to store some data (!!!). These must be filtered out, else the addresses used for reading/writing will be wrong. This really sucks as this means that in every memory operation (read/write) these bits must be ANDed out.
The memory map will be :
  • 0 ... X : usable RAM
  • X+1 ... Y : linkage block
  • Y+1 ... Z : Video Ram
  • Z+1 ... : "ROM" i.e. the OS: may be read from but not written to (except special case).
  • Field Details

    • externalInterruptHandlersList

      protected ExternalInterruptHandler[] externalInterruptHandlersList
    • interruptSignaled

      protected boolean[] interruptSignaled
    • interruptLevel

      protected int[] interruptLevel
    • nbrOfInterruptHandlers

      protected int nbrOfInterruptHandlers
    • newInterruptGenerated

      protected boolean newInterruptGenerated
    • trapDispatcher

      public TrapDispatcher trapDispatcher
    • mainMemory

      protected final char[] mainMemory
    • CUT_OFF

      public static final int CUT_OFF
      See Also:
    • screenStart

      protected int screenStart
    • screenStop

      protected int screenStop
    • screen

      public Screen screen
    • ramSize

      public int ramSize
    • totRamSize

      protected int totRamSize
    • totRamSizeForLong

      protected int totRamSizeForLong
    • totRamSizeForWord

      protected int totRamSizeForWord
    • totMemSize

      protected int totMemSize
    • data_regs

      public final int[] data_regs
    • addr_regs

      public final int[] addr_regs
    • pc_reg

      public int pc_reg
    • reg_sr

      public int reg_sr
    • reg_usp

      public int reg_usp
    • reg_ssp

      public int reg_ssp
    • currentInstructionAddress

      protected int currentInstructionAddress
    • disasmBuffer

      protected StringBuilder disasmBuffer
    • i_table

      protected final Instruction[] i_table
    • ieh

      protected ExternalInterruptHandler ieh
    • INTERRUPT_FLAGS_MASK

      public static final int INTERRUPT_FLAGS_MASK
      See Also:
    • SUPERVISOR_FLAG

      public static final int SUPERVISOR_FLAG
      See Also:
    • TRACE_FLAG

      public static final int TRACE_FLAG
      See Also:
    • stopNow

      public int stopNow
    • pcs

      public int[] pcs
  • Constructor Details

    • MC68000Cpu

      public MC68000Cpu(int size, inifile.IniFile iniFile, int romSize, int videoRamSize)
      Creates the object.
      Parameters:
      size - the size of ram in bytes.
      iniFile - my .ini file with options to set.
      romSize - the size of the ROM image. Loaded after the RAM image.
      videoRamSize - size,in bytes, of screen memory to be allocated here.
    • MC68000Cpu

      public MC68000Cpu(int size, Screen screen, inifile.IniFile iniFile, int romSize)
      Creates the object.
      Parameters:
      size - the size of ram in bytes. This is NOT allocated when the object is created, but only when the OS is loaded.
      screen - the screen attached to the machine.
      iniFile - my .ini file with options to set.
      romSize - the size of the ROM image. Loaded above RAM and VRAM.
  • Method Details

    • execute

      public final int execute()
      Executes one single instruction.
      Returns:
      negative values for comm with the Java prog or 0 : all went OK.
    • executeContinuous

      public final void executeContinuous()
      Continuous execution loop. This is the fastest way to execute an MC 68000 prog.
    • generateInterrupt

      public void generateInterrupt()
      This is called by an external level 2 interrupt handler wishing to signal an external interrupt. It is presumed that this is a level 2 interrupt!
    • endEmulation

      public void endEmulation()
      Kill the emulation thread by returning from the exeuction loop at next loop iteration
    • registerInterruptHandler

      public void registerInterruptHandler(ExternalInterruptHandler eh)
      Registers an external interrupt handler with the CPU.
      Parameters:
      eh - the ExternalInterruptHandler to register
    • readMemoryByte

      public final int readMemoryByte(int address)
      Reads an unsigned byte from memory.
      Parameters:
      address - where to read from.
      Returns:
      an int containing the unsigned byte read.
    • readMemoryByteSigned

      public final int readMemoryByteSigned(int address)
      Reads a signed byte from memory.
      Parameters:
      address - where to read from.
      Returns:
      an int containing the sign extended byte read.
    • readMemoryWord

      public final int readMemoryWord(int address)
      Reads an unsigned word from memory. This will give a wrong result if the address is uneven.
      Parameters:
      address - where to read from.
      Returns:
      an int containing the unsigned word read.
    • readMemoryWordSigned

      public final int readMemoryWordSigned(int address)
      Reads a word and returns it as signed int. This will give a wrong result if the address is uneven.
      Parameters:
      address - where to read from.
      Returns:
      a sign extended int containing the signed word read.
    • readMemoryWordPC

      public final int readMemoryWordPC()
      Reads an unsigned word at address (PC).
      Returns:
      an int containing the unsigned word read.
    • readMemoryWordPCInc

      public final char readMemoryWordPCInc()
      Reads an unsigned word at address (PC) and increases PC by 1.
      Returns:
      an int containing the unsigned word read.
    • readMemoryWordPCSigned

      public final int readMemoryWordPCSigned()
      Reads a word at address PC and returns the word as a signed int. This will give a wrong result if the address is uneven.
      Returns:
      an int containing the signed word read.
    • readMemoryWordPCSignedInc

      public final int readMemoryWordPCSignedInc()
      Reads a word at address PC, increases PC by 1 and returns the word as a signed int. This will give a wrong result if the address is uneven.
      Returns:
      a signed int containing the signed word read.
    • readMemoryShort

      public final short readMemoryShort(int address)
      Reads a word as signed short. This will give a wrong result if the address is uneven.
      Parameters:
      address - where to read from.
      Returns:
      a short containing the unsigned word read.
    • readMemoryChar

      public final char readMemoryChar(int address)
      Reads a word as signed short. This will give a wrong result if the address is uneven.
      Parameters:
      address - where to read from.
      Returns:
      a short containing the unsigned word read.
    • readMemoryLong

      public final int readMemoryLong(int address)
      Reads a long word.
      Parameters:
      address - where to read from.
      Returns:
      an int containing the long word read.
    • readMemoryLongPC

      public final int readMemoryLongPC()
      Reads a long word at (PC).
      Returns:
      an int containing the long word read.
    • readMemoryLongPCinc

      public final int readMemoryLongPCinc()
      Reads a long word at (PC) and increases the PC by 2 (actually by 4).
      Returns:
      an int containing the long word read.
    • writeMemoryByte

      public void writeMemoryByte(int address, int value)
      Writes a byte to memory.
      Parameters:
      address - where to write to.
      value - the byte to write (in the LSB of the int).
    • writeMemoryWord

      public void writeMemoryWord(int address, int value)
      Writes a word to memory.
      Parameters:
      address - where to write to.
      value - the word to write (in the LSW of the int).
    • writeMemoryShort

      public void writeMemoryShort(int address, short value)
      Writes a short to memory.
      Parameters:
      address - where to write to.
      value - the short to write.
    • writeMemoryWord

      public void writeMemoryWord(int address, char value)
      Writes a char to memory.
      Parameters:
      address - where to write to.
      value - the short to write.
    • writeMemoryChar

      public void writeMemoryChar(int address, char value)
      Writes a char to memory.
      Parameters:
      address - where to write to.
      value - the short to write.
    • writeMemoryLong

      public void writeMemoryLong(int address, int value)
      Writes a long to the memory.
      Parameters:
      address - the address where to write to.
      value - the value to write.
    • debugMem

      protected void debugMem(int add, int value)
      For debugging only.
      Parameters:
      add - address to check
      value - value to check
    • reset

      public void reset()
      Resets this CPU. This puts the all regs to 0, except for the status reg, put at $2700 (spuervisor mode, no interrupts).
    • raiseException

      public void raiseException(int vector)
      Raises the exception of that vector.
      Parameters:
      vector - the exception vector to raise.
    • raiseSRException

      public void raiseSRException()
      Special Status register exception.
    • setSR

      public void setSR(int value)
      Sets the status register with a certain value.
      Parameters:
      value - the value to set.
    • testTrace

      public void testTrace()
      Checks the trace flag.
    • signExtendByte

      public static final int signExtendByte(int value)
      Sign extend a byte.
      Parameters:
      value - the int containing the byte to be sign extended.
      Returns:
      the sign extended int.
    • signExtendWord

      public static final int signExtendWord(int value)
      Sign extend a word.
      Parameters:
      value - the int containing the word to be sign extended.
      Returns:
      the sign extended int.
    • isSupervisorMode

      public boolean isSupervisorMode()
    • getDisplacement

      public final int getDisplacement()
      Gets the displacement and increments the PC.
      Returns:
      the displacement
    • addInstruction

      public void addInstruction(int opcode, Instruction i)
      Adds an instruction to the list (array) of instructions
      Parameters:
      opcode - the opcode of the instruction to be added to the cpu's list of instructions. This is the index into the instructions array.
      i - the instruction to add.
    • disassembleSrcEA

      public DisassembledOperand disassembleSrcEA(int address, int mode, int reg, Size sz)
    • disassembleDstEA

      public DisassembledOperand disassembleDstEA(int address, int mode, int reg, Size sz)
    • disassembleEA

      protected DisassembledOperand disassembleEA(int address, int mode, int reg, Size sz, boolean is_src)
    • getInstructionFor

      public Instruction getInstructionFor(int opcode)
      Gets the instruction for an opcode.
      Parameters:
      opcode - the opcode for which to get the instruction.
      Returns:
      the instruction for this opcode.
    • setZStatusFlag

      public void setZStatusFlag()
      Sets/clears the Z flag according to D0.
    • testD0

      public void testD0()
    • getScreenAddresses

      public int[] getScreenAddresses()
      Gets the address of the start and end of the screen.
      Returns:
      an array with the address of the start and end of the screen.
    • moveBlock

      public void moveBlock()
      Move a block of pixels around (for 8 and 16 bit modes only!). Do not use for QL screen mode! Remember the VRAM is part of the mainMemory array, so moving pixels about is just moving bytes/words in the main memory. Attention however, the block size is given in "pixels". This means that, in mode 32, each pixel represents a word, which suits me just dandy as my mem is in words, too. In 8 bit mode, however, each pixels is represented by a byte. So, if any X parameter (either size or origs) is not even, I chicken out and let SMSQ/E handle the move. If the move is handled here directly, this also does the RTS. The return to MSQ/E is thus either right after the call to this routine, with the Z flag unset, or to the caller of the mblock routine in SMSQ/E, with the Z flag set.
    • copyMem

      public boolean copyMem(int srcStart, int destStart)
      Copies a block of memory within the main memory or within or to/from screen.!!USE ONLY IN 8 OR 16 BIT COLOUR MODES !!! The source and the destination might overlap.
      Parameters:
      srcStart - (A4)= base address of source
      destStart - (A5)= base address of destination
      Returns:
      true if operation was done, false if pb
    • combineBlocks

      public void combineBlocks()
      This combines two blocks (source 1, source2) with alpha blending and puts the result into the destination. The destination MUST be the screen, else this returns to SMSQ/E with an out-of-range error in D0. Source2 must have a row increment of destination row increment + 4.
    • setCopyScreen

      public void setCopyScreen(int QLScreenMode, int origins)
      This switches QL screen emulation on or off. If on, any writes to the QL screen area are copied to the true display. This ALWAYS FAILS HERE with 'not implemented' error.
      Parameters:
      QLScreenMode - the screen mode the QL screen is supposed to be in.
      origins - origins in y screen which should correspond to (0,0) in QL screen.
    • writeToFile

      public int writeToFile(int address, int nbrOfBytes, FileChannel outChannel) throws IOException
      Write bytes read from memory to a filechannel.
      Parameters:
      address - where to start reading.
      nbrOfBytes - how many bytes to write.
      outChannel - the channel to write to.
      Returns:
      nbr of bytes written to channel.
      Throws:
      IOException - from the file channed I/O operations.
    • writeToBuffer

      public ByteBuffer writeToBuffer(int offsetInBuffer)
      Creates a ByteBuffer and writes bytes read from the memory to it. The start address where to take the bytes from and length (nbr of bytes to get) are in the registers A1 and D2 (long)
      Parameters:
      offsetInBuffer - nbr of bytes to add to the size, and also start of where to put these bytes in the newly created buffer.
      Returns:
      the newly created ByteBuuffer or null if there was an error. NOTE THIS PRESUMES THAT THE START ADDRESS IS EVEN
    • writeToBuffer

      public int writeToBuffer(ByteBuffer buffer, int offsetInBuffer)
      Writes bytes read from the EVEN memory to a ByteBuffer. The start address where to take the bytes from and length (nbr of bytes to get) are in the registers A1 and D2 (long).

      NOTE THIS PRESUMES THAT THE START ADDRESS IS EVEN!

      Parameters:
      buffer - the ByteBuffer into which the bytes read from memory are to be written.
      offsetInBuffer - nbr of bytes to add to the size, and also start of where to put these bytes in the newly created buffer.
      Returns:
      nbr of bytes written, 0 if error
    • writeToBuffer

      public int writeToBuffer(ByteBuffer buffer, int A1, int bytesToWrite)
      Writes bytes read from the memory to a ByteBuffer. The bytes will be put at the current position in the buffer. THE LIMIT oF THIS BUFFER WILL BE SET TO ITS CAPACITY.
      Parameters:
      buffer - the ByteBuffer into which the bytes read from memory are to be written at the current position of this buffer. THE LIMIT oF THIS BUFFER WILL BE SET TO ITS CAPACITY.
      A1 - where to start reading the bytes from.
      bytesToWrite - nbr of bytes to write to the buffer.
      Returns:
      nbr of bytes written, -1 if buffer doesn't contain enough space. In that case NOTHING is written to the buffer.
    • readFromFile

      public int readFromFile(int address, int nbrOfBytes, FileChannel inChannel) throws IOException
      Read bytes from a filechannel and write them into memory.
      Parameters:
      address - where to read to.
      nbrOfBytes - how many bytes to read.
      inChannel - the channel to read from.
      Returns:
      nbr of bytes read.
      Throws:
      IOException - from the file channed I/O operations.
    • readFromFile

      public int readFromFile(int address, int nbrOfBytes, FileChannel inChannel, boolean specialRead) throws IOException
      Read bytes from a filechannel and write them into memory. This creates a temporary ByteBuffer, reads the file into it and then copies from that ByteBuffer into the memory
      Parameters:
      address - where to read to.
      nbrOfBytes - how many bytes to read.
      inChannel - the channel to read from.
      specialRead - =true ONLY when loading the OS, must be false at all other times!
      Returns:
      nbr of bytes read, -1if EOF
      Throws:
      IOException - from the file channed I/O operations.
    • readFromBuffer

      protected int readFromBuffer(int address, int nbrOfBytes, ByteBuffer buffer, int startInBuffer, boolean specialRead)
      Reads bytes from a ByteBuffer and writes them into memory.
      Parameters:
      address - where to read to.
      nbrOfBytes - how many bytes are to be read.
      buffer - the buffer to read from.
      startInBuffer - where in the buffer to start reading from.
      specialRead - =true ONLY when loading the OS, must be false at all other times!
      Returns:
      nbr of bytes read.
    • readFromBuffer

      public int readFromBuffer(int address, int nbrOfBytes, ByteBuffer buffer, int startInBuffer)
      Reads bytes from a buffer and writes them into memory.
      Parameters:
      address - where to read to.
      nbrOfBytes - how many bytes to read.
      buffer - the buffer to read from.
      startInBuffer - the first byte in the buffer to read from.
      Returns:
      nbr of bytes read ,-1 if EOF
    • findInMemory

      public int findInMemory(int startAddress, int stopAddress, String toFind)
      Finds a string in memory. The length of the string MUST be even.
      Parameters:
      startAddress - where to start searching.
      stopAddress - where to stop searching.
      toFind - the string to find, must have an even number of characters.
      Returns:
      the address where the first short can be found, -1 if none.
    • findInMemory

      public int findInMemory(int startAddress, int stopAddress, short[] toFind, String addString)
      Finds consecutive shorts followed by a string in memory.
      Parameters:
      startAddress - where to start searching.
      stopAddress - where to stop searching.
      toFind - the array with the consecutive shorts to find, in the order they are in that array.
      addString - th additional string to find, the length of it MUST BE EVEN
      Returns:
      the address where the first short can be found, -1 if none.
    • findInMemory

      public int findInMemory(int startAddress, int stopAddress, short[] toFind)
      Finds consecutive shorts in memory.
      Parameters:
      startAddress - where to start searching.
      stopAddress - where to stop searching.
      toFind - the array with the consecutive shorts to find, in the order they are in that array.
      Returns:
      the address where the first short can be found, -1 if none.
    • setTrapDispatcher

      public void setTrapDispatcher(TrapDispatcher td)
      Sets the trap dispatcher for this CPU, which is called whenever a JAVAComm instruction (such as for some device drivers) is encountered.
      Parameters:
      td - the trap dispatcher
    • setKeyrow

      public void setKeyrow(int row, int col)
      Sets the keyrow parameter for this key (row and col).
      Parameters:
      row - what row (0 to 7)
      col - what col (0 to 7)
    • removeKeyrow

      public void removeKeyrow(int row, int col)
      Unsets the keyrow parameter for this key (row and col).
      Parameters:
      row - what row (0 to 7)
      col - what col (0 to 7)
    • removeAllKeyrows

      public void removeAllKeyrows()
      Unsets all keyrows.
    • setScreenMode

      public void setScreenMode(int mode)
      Sets the display mode for a QL compatible screen, a fall through to the corresponding screen routine.
      Parameters:
      mode - (0 or 8)
    • setEmuScreenMode

      public void setEmuScreenMode(int mode)
      Sets the display mode when emulating a QL compatible screen, a fall through to the corresponding screen routine.
      Parameters:
      mode - (0 or 8)
    • readableSize

      public int readableSize()
      Gets the highest readable (not necessarily writable) memory location.
      Returns:
      the highest readable (not necessarily writable) memory location, i.e. the end of the "ROM".
    • getLinkageBlock

      public int getLinkageBlock()
      Gets the address of the start of the linkage block = end of RAM.
      Returns:
      the address of the start of the linkage block
    • getScreen2

      public Screen getScreen2()
      Gets the screen used by this object.
      Returns:
      the screen used.
    • getRomFile

      public String getRomFile()
      Returns the name of the last romfile loaded.
      Returns:
      the name of the last romfile loaded, or null if none.
    • getMemory

      public char[] getMemory()
      Gets the memory used by this cpu for some sort of "dma". Be careful what you do with this.
      Returns:
      the memory (array of short) used by the cpu.
    • getRamSizeInMiB

      public int getRamSizeInMiB()
      Gets the ram size in MB.
      Returns:
      ram size in MB.
    • getRamSize

      public int getRamSize()
      Gets the ram size in MB.
      Returns:
      ram size in MB.
    • getTotalRamSize

      public int getTotalRamSize()
      Gets the total ram size.
      Returns:
      ram size in MB.
    • getTotalMemSize

      public int getTotalMemSize()
      Gets the total ram size.
      Returns:
      ram size in MB.
    • getScreenStart

      public int getScreenStart()
    • setScreen

      public void setScreen(Screen scree)