How to use cpu4 simulator

command line    cpu4 < input-S-format

The simulator takes object code input file in S-format.  It executes the instructions step by step and shows all the registers and flags.  A jump to subroutine 1001 (JSR 1001) will stop the simulator.  JSR 1000 prints the content of AC.   The variable "trace" controls the trace mode.  To turn off the trace assign "trace = 0" in the main() and recompile the simulator.

In "run()" the limit of the number of instruction executed is set to 500 to prevent an infinite loop.  This number can be changed by recompiling the simulator.

The simulator implements a Big Endian representation ( Hi byte first).  When pushing 16 bits value into the stack, Lo byte will be pushed first then Hi byte  (so that the number in the data segment and stack segment will be ordered in the same way).

The output in trace mode displayed PC, instruction, AC, SP, and flags.  All numbers are in decimal.   For example : running this program will display

.s
stop 1001
.a 0
.c
 lda #1
 rol
 lda #254
 rol
 lda #2
 ror
 lda #3
 ror
 jsr  stop
.e
 
pc:0000 LDA  A:01  SP:03E7  C0 Z0
pc:0002 ROL  A:02  SP:03E7  C0 Z0
pc:0003 LDA  A:FE  SP:03E7  C0 Z0
pc:0005 ROL  A:FC  SP:03E7  C1 Z0
pc:0006 LDA  A:02  SP:03E7  C0 Z0
pc:0008 ROR  A:01  SP:03E7  C0 Z0
pc:0009 LDA  A:03  SP:03E7  C0 Z0
pc:000B ROR  A:01  SP:03E7  C1 Z0
stop
 

You can get a better idea how each instruction work by looking at the source code of the simulator.  Especially how to set Carry flag and ROR, ROL.

Please report bugs to prabhas@chula.ac.th