Recursos para o Livro Arquitetura e Organização de Computadores

Gabriel P. Silva & José Antonio S. Borges - Editora LTC

SimuS

Sapiens Processor Simulator

User Manual


1. Introduction

SimuS (Sapiens Simulator) is an educational tool developed to simulate the operation of the Sapiens processor. This simulator allows students to write, compile, and execute programs in assembly language, visualizing the behavior of the processor, registers, flags, and memory in real time.

SimuS offers advanced debugging features, including step-by-step execution, breakpoints, memory visualization, and simulated input/output ports.


2. User Interface

The user interface language can be set to Portuguese, English, or Spanish by clicking one of the flags located in the upper corner of the main window.

The SimuS interface is divided into three main panels, each with specific functions to facilitate program development and debugging.

2.1. Left Panel - Editor and Execution

This panel contains three tabs that allow you to edit code, view errors, and monitor execution:

2.1.1. Editor Tab

An assembly code editing area with the following features:

2.1.2. Errors Tab

Displays error messages generated during compilation:

2.1.3. Execution Tab

Visualization of the compiled code with debugging resources:

2.2. Central Panel - CPU and Controls

This panel concentrates all execution controls and the visualization of the CPU state:

2.2.1. Control Buttons

Five distinctly colored buttons to control execution:

2.2.2. Status Bar

Displays the current state of the simulator in gray text below the control buttons. Messages include: “Ready”, “Running…“, “TURBO - Running…“, “HALT - Program Finished”, among others.

2.2.3. Registers

Three boxes displaying the values of the main registers in hexadecimal:

2.2.4. Current Instruction

A box with a dark background displaying the mnemonic of the instruction currently pointed to by the PC. Example: LDA #FF. This yellow-gold highlighted view makes it easy to track execution.

2.2.5. Flags (Indicators)

Three visual indicators showing the state of the processor flags:

2.2.6. Input/Output Ports

Simulated peripheral interface with four components:

2.3. Right Panel - Memory Visualization

Displays the contents of the RAM memory in hexadecimal format with navigation features:

2.3.1. Navigation Bar

Controls at the top of the memory panel:

2.3.2. Memory Grid

Visualization in a hexadecimal grid with the following characteristics:


3. File Toolbar

Located at the top of the left panel, it offers three buttons for file management:


4. Typical Workflow

Follow these steps to develop and execute programs in SimuS:

  1. Edit the Code: In the Editor tab, write your assembly program. Use comments (;) to document the code. Remember to include the ORG directive to define the starting address and END to mark the end of the program.

  2. Compile: Click the ✔ Compile button. If there are errors, correct them as indicated in the Errors tab and compile again.

  3. Set Breakpoints (Optional): In the Execution tab, click on the lines where you want to pause execution. A red circle will appear on the address.

  4. Run: Choose an execution mode:
    • Step: For detailed, instruction-by-instruction debugging.
    • Run: For normal speed execution with visual updates.
    • Turbo: For long programs, running at maximum speed.
  5. Monitor Execution: Observe the registers, flags, memory, and I/O ports updating in real time. The current line is highlighted in yellow in the Execution tab.

  6. Interact with I/O: When the program executes IN 0, type a hexadecimal value into the Input (Hex) field and press ENTER. Values sent via OUT are displayed automatically.

  7. Finish: The program stops automatically when it encounters an HLT. Use the Reset button to restart and run again.

  8. Save: Use 💾 Save As… to save your work to a file.

5. Sapiens Instruction Set

The Sapiens processor implements the following categories of instructions:

5.1. Data Transfer Instructions

Mnemonic Name Description
LDA Load Accumulator Loads AC with a value from memory or an immediate value
STA Store Accumulator Stores AC into memory
LDS Load Stack Pointer Loads SP with a 16-bit value
STS Store Stack Pointer Stores SP into memory (16 bits)

5.2. Arithmetic Instructions

Mnemonic Name Description
ADD Addition AC = AC + operand (updates flags)
ADC Add with Carry AC = AC + operand + C (updates flags)
SUB Subtraction AC = AC - operand (updates flags)
SBC Subtract with Carry AC = AC - operand - C (updates flags)

5.3. Logical Instructions

Mnemonic Name Description
AND Logical AND AC = AC & operand (updates flags)
OR Logical OR AC = AC | operand (updates flags)
XOR Exclusive OR AC = AC ^ operand (updates flags)
NOT Logical NOT AC = ~AC (updates flags)

5.4. Shift Instructions

Mnemonic Name Description
SHL Shift Left Shifts AC 1 bit to the left (bit 0 = 0, C receives bit 7)
SHR Shift Right Shifts AC 1 bit to the right logically (bit 7 = 0, C receives bit 0)
SRA Shift Right Arithmetic Shifts AC 1 bit to the right arithmetically (maintains bit 7)

5.5. Flow Control Instructions

Mnemonic Name Description
JMP Jump Jumps unconditionally to an address
JZ Jump if Zero Jumps if flag Z = 1
JNZ Jump if Not Zero Jumps if flag Z = 0
JN Jump if Negative Jumps if flag N = 1
JP Jump if Positive Jumps if flag N = 0
JC Jump if Carry Jumps if flag C = 1
JNC Jump if No Carry Jumps if flag C = 0

5.6. Subroutine Instructions

Mnemonic Name Description
JSR Jump to Subroutine Saves PC to the stack and jumps to the subroutine
RET Return Returns from a subroutine (recovers PC from the stack)

5.7. Stack Instructions

Mnemonic Name Description
PUSH Push to Stack Pushes AC onto the stack (mem[SP] = AC, then SP–)
POP Pop from Stack Pops from the stack to AC (SP++, then AC = mem[SP])

5.8. Input/Output Instructions

Mnemonic Name Description
IN Input Reads data from the specified port into AC
OUT Output Sends AC to the specified port

Available I/O Ports:

Instruction Function
IN 0 Reads the hexadecimal value typed by the user
IN 1 Reads input status (1 = data available, 0 = no data)
OUT 0 Sends AC to the hexadecimal output display
OUT 2 Clears the text banner
OUT 3 Sends an ASCII character to the text banner (appends to the end)

5.9. Special Instructions

Mnemonic Name Description
NOP No Operation Does nothing (can be used for timing)
HLT Halt Stops the processor execution
TRAP Trap Generates a service call

Available TRAP Operations:

Instruction Function
#0 Clears the console terminal
#11 Reads a character from the terminal and saves it in the accumulator and the operand’s memory address
#2 Writes a character from the operand’s memory address to the terminal
#3 Reads a string from the terminal and saves it at the operand’s memory address
#4 Writes a string starting from the operand address (until a NULL is found)
#5 Delay (Waits from 0 to 65535 ms)
#6 Beep (Audio Synthesizer). Receives frequency and duration as parameters
#7 Returns a pseudo-random number between 0 and 99 in the accumulator

6. Addressing Modes

Sapiens supports four addressing modes, identified by the 2 most significant bits of the opcode:

Bits Mode Example Operation Description
00 Direct LDA 50 AC = mem[50] The operand is the memory address of the data
01 Indirect LDA @50 AC = mem[mem[50]] The operand points to an address containing the final address of the data
10 8-bit Immediate LDA #10 AC = 10 The operand is the byte following the instruction
11 16-bit Immediate LDS #1000 SP = 1000 The operand consists of the two bytes following the instruction

Notes on Addressing:


7. Operand Formats

Sapiens supports several types of formats for instruction operands:


8. Assembler Directives

The SimuS assembler recognizes the following directives:

Directive Description Example Effect
ORG address Defines the starting address of the program ORG 0 Program starts at address 0
END Marks the end of the source code END Last line of the file
DB value Defines a byte (8 bits) DB #FF Stores the byte FF in memory
DW value Defines a word (16 bits) DW #1234 Stores the word 1234 (little-endian)
DS quantity Defines space (reserves bytes) DS 10 Reserves 10 zeroed bytes
LABEL: EQU value Defines a constant (: is optional) TESTE: EQU 10 TESTE will be equal to 10

Use of Labels:

Labels can be defined before any instruction or directive, ending with a colon:

LOOP:
    LDA VALUE
    JNZ LOOP


9. Code Examples

9.1. Simple Echo

A program that waits for user input and echoes the value back:

; Echo Program
ORG 0
LOOP:
    IN 1          ; Read status
    JZ LOOP       ; Wait for data
    IN 0          ; Read value
    OUT 0         ; Display value
    HLT
END

9.2. Counter from 0 to 9

Counts from 0 to 9 and stops:

; Counter
ORG 0
    LDA #0        ; Start at 0
LOOP:
    OUT 0         ; Show value
    ADD #1        ; Increment
    SUB #10       ; Compare with 10
    JNZ LOOP      ; Continue if != 10
    HLT
END

9.3. Subroutine with Stack

Demonstrates the use of JSR, RET, and PUSH/POP:

; Subroutine
ORG 0
    LDA #42
    JSR SAVE      ; Call subroutine
    OUT 0         ; Show result
    HLT

SAVE:
    PUSH          ; Save AC
    LDA #99
    OUT 0         ; Show 99
    POP           ; Restore AC (42)
    RET
END


10. Troubleshooting Common Issues

Error: “Invalid Instruction”

Error: “Undefined Label”

Program does not execute after compiling

Breakpoint does not work

Memory does not update during execution

IN does not read the typed value


11. Tips and Best Practices


12. Conclusion

SimuS is a complete tool for learning computer architecture and assembly programming. Through its intuitive interface and advanced debugging features, students can experiment with fundamental concepts such as:

This manual covers the essential aspects of the simulator. For additional questions or technical support, consult the Sapiens processor documentation or contact the developer.

Happy studying and happy programming!