Question
Create a command line application in C called siavm that takes as a parameter a filename (argv[1]). It should call memorys load function then call
Create a command line application in C called siavm that takes as a parameter a filename (argv[1]). It should call memorys load function then call the run loop.
IMPORTANT: Typically, students are taught to NEVER use global variables, to pass values as function parameters and to NOT hardcode values. FOR THIS PROJECT, DISREGARD ALL OF THESE RULES. The fetch, decode, execute and store functions should accept no parameters and should return void.
Memory
Allocate space for your virtual machines memory. 1k as unsigned byte should be enough. This can be done on the stack, globally.
Create a load function that takes a filename; it should read the file into the virtual machines memory starting at the beginning. This file should be the OUTPUT from your assembler.
CPU infrastructure
Create 16 registers. Create a PC internal register. Create OP1 and OP2 and Result internal registers. Create a current instruction register. Prepopulate R15 to the bottom of the stack.
Create a run/event loop that calls fetch, decode, execute, store until a flag indicating that HALT occurred.
Fetch
Read instruction bytes from memory.
Decode (also known as dispatch)
Read only from the array of bytes from fetch. Populate the OP1 and OP2 registers if appropriate. Fetch any additional memory needed to complete the instruction.
Execute
Switch statement that does the work and stores the work into Result
Store
Write result into final register or memory address. Update PC.
this is SIA instruction
Simple Instruction Architecture
Instructions
3R instructions
3R
4 bits | 4 bits | 4 bits | 4 bits |
OPCODE | register 1 | register 2 | destination (register 3) |
add (opcode 1)
Adds the values of 2 registers and places the answer in a third register.
Example: add r1 r2 r3 ; r3 r1 + r2
Instruction format: 3R
and (opcode 2)
Preforms a bitwise and on 2 registers and stores the result in a third register
Example: and r1 r2 r3 ; r3 r1 & r2
Instruction format: 3R
divide (opcode 3)
halt (opcode 0)
multiply (opcode 4)
or (opcode 6)
subtract (opcode 5)
Branch instructions (ALL OPCODE 7)
br1
4 bits | 4 bits | 4 bits | 4 bits |
OPCODE | Branch type | register 1 | register 2 |
16 bits |
16 bits of address offset |
OR
br2
4 bits | 4 bits | 8 bits |
OPCODE | Branch type | Top 8 bits of address |
16 bits |
Bottom 16 bits of address |
branchXXXXXXXXX (opcode 7)
Conditionally branches based on register 1 and register 2. There are 6 branch instructions:
Command | Branch Type | Condition |
branchIfLess | 0 | if (r1 < r2) |
branchIfLessOrEqual | 1 | if (r1 <= r2) |
branchIfEqual | 2 | if (r1 == r2) |
branchIfNotEqual | 3 | if (r1 != r2) |
branchIfGreater | 4 | if (r1 > r2) |
branchIfGreaterOrEqual | 5 | if (r1 >= r2) |
If the condition is true, jump to an offset from the current program counter. (The offset is the number of words (2 bytes) forward or back. PC <= PC + (2 * offset).
Example: branchifequal r1 r2 1000
Instruction format: br1
call (opcode 7)
Calls a function pushes the PC of the next instruction onto the stack (R15), then jumps to the address specified by this instruction times 2.
Example: call 444
Instruction format: br2, branch type 6
jump (opcode 7)
Jumps to the location specified in the instruction times 2
Example: jump 1000
instruction format: br2, branch type 7
Load/Store instructions
ls
load (opcode 8)
Loads a register from the memory pointed to by another register plus 2 times the offset (0 to 30). Note that both the address in the register and the offset are in words (memory locations).
Example: load r1 r2 10 ; loads r1 with the value pointed to by r2 plus 20 bytes
instruction format: ls
store (opcode 9)
Stores a registers value into memory pointed to by another register plus 2 times the offset (0 to 30). Note that both the address in the register and the offset are in words (memory locations).
Example: store r1 r2 10 ; stores r1s value into the memory pointed to by r2 plus 20 bytes
instruction format: ls
Stack Instructions
stack
Copies data from stack pointer through stack pointer + 3 to specified register. Adds four to the stack pointer.pop (opcode 10)
Example: pop R1
Instruction format: stack
push (opcode 10)
Subtracts four from the stack pointer. Takes the value in the specified register and stores it in the memory address indicated by the stack pointer.
Example: push R1
Instruction format: stack
return (opcode 10)
Pops the top value from the stack and jumps to that address. Register is ignored for return.
Example: return
Instruction format: stack
Move
move (opcode 11)
Sets a register to a signed 8 bit value
Example: move -127 r1; sets register R1 to -127
Instruction format: move
interrupt (opcode 12)
Interrupts the CPU using a particular interrupt number. This could be used to jump between kernel mode and user mode or to support devices. For the virtual machine, two interrupts are supported: 0 (print registers) and 1 (print out memory).
Example: interrupt 17
Instruction format: move Note that the register is ignored.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started