Apr 6 - Illinois Institute of Technology

Illinois Institute of Technology
Lab 8 and Final Project
An LC-3 Simulator
CS 350: Computer Organization & Assembler Language Programming
Lab 8 due Fri Apr 24 / Final Project due Sat May 2
[5/2: p.3, 5]
A. Why?
• Implementing a computer really helps you understand its structure.
B. Outcomes
After this project, you should
• Know how to implement the major parts of a computer in a higher-level
language.
• Know how to access structures via pointers in C.
C. Project
• You are to implement of a simple console-oriented simulator for the LC-3 in C
using your SDC simulator from Lab 5 as a starting point. Your program
should take the name of a *.hex file as a command line parameter, load it
into memory, initialize the control unit, and run a command loop. Commands
can cause program execution and other functions.
D. Input File
• As in the SDC simulator, the input file should be a command line parameter.
If it’s omitted, use default.hex as the default filename.
• You should be able to process a *.hex file produced by the LC-3 editor when
it assembles a *.asm file.
• A *.hex file is a text file containing a sequence of lines with a four-digit
hex number on each line (no leading x or 0x).
• The first line specifies the .ORIG location to start loading values into.
The remaining lines contain the values to load.
• If you read a value into location xFFFF, wrap around to x0000 as the
next location.
CS 350: Comp Org & Asm Pgm’g
–1 –
© James Sasaki, 2015
Illinois Institute of Technology
Lab 8 and Final Project
• If anything appears on a line after the four-digit hex number, ignore it.
(This will let us add comments to our hex files.)
• Note: The LC-3 text editor will not allow Translate → Convert Base
16 on a *.hex file augmented with comments.
• All other memory locations should be set to the value zero. In particular,
don’t simulate the TRAP table in low memory (x00 – xFF) or the traphandling code in upper memory.
E. The Command Loop
• Once the program is read into memory, initialize the PC to the .ORIG value,
the IR to zero, the condition code to Z, the running flag to true, and start the
command loop.
• The user should enter a carriage return after each command (so h\n, for
example). Below, addresses and values should be hex numbers starting with
an x and containing 1 to 4 hex digits, as in x0, x12, xffff.
Simulator Commands
Command
Syntax
Action
Help
? or h
Print a summary of the simulator commands
Dump
d
Print the contents of the CPU (CU & Memory)
Go to
g address
Set PC ← address and running flag ← true
Set Memory
s address value
Set memory[address] ← value
Set Register
s rN value
Set register N ← value (N = 0, …, 7)
Execute
integer
Execute that many instruction cycles
Execute
(none: just the \n)
Equivalent to the integer 1 (execute one cycle)
Quit
q
End the simulation
Notes
• The commands have changed from last semester.
CS 350: Comp Org & Asm Pgm’g
–2 –
© James Sasaki, 2015
Illinois Institute of Technology
Lab 8 and Final Project
• For the dump command, print out the control unit (PC, IR, CC, running flag,
and data registers) and print out the values in memory that are nonzero.
(Don’t print 216 lines.) [updated 5/2]
• For the numeric execute command, the number of cycles to run should be a
positive decimal integer. (If not, complain and go on to the next command.)
If execution has already halted (the CPU running flag is false), say so and go
on to the next command. If the number of cycles to run is way too large (say,
> 100), complain and use 100 instead. If execution halts as you run the
cycles, stop early and go on to the next command.
• The HALT trap stops execution but does not stop the command loop. This lets
you enter d/g/s/h commands after the program finishes. Since the g
command turns the CPU running flag on, you can use it to restart program
execution after a HALT.
F. Command Format
• Case Sensitivity:
• You can assume the command names ?, h, d, q, s are in lower case (with
extra credit for also allowing upper case).
• You can assume the leading r in a register name (r0, r1, …, r7) or x in a
hex number (as in x1234) is in lower case (with extra credit for also
allowing upper case).
• For hexadecimal numbers, sscanf allows upper and lower a–f as digits,
so you should too.
• Whitespace: Let, “whitespace” mean a sequence of one or more spaces or tabs.
• Whitespace is not allowed inside a token (an address, register, or value).
Let . stand for a space; then g.x8000\n is legal but not g.x.8000\n.
• Whitespace is allowed before the command but is required between
tokens. E.g., g.x1234\n and ..g.\t.x1234\n are equivalent.
• Whitespace can appear before the \n of the empty (execute one
instruction cycle) command. E.g., \n and ..\n are equivalent.
CS 350: Comp Org & Asm Pgm’g
–3 –
© James Sasaki, 2015
Illinois Institute of Technology
Lab 8 and Final Project
• Commands Are One Line Long: For commands g and s, do not read the g or
s separately and expect the user to enter more of the command: g\n is an
error, not a signal for you to prompt the user.
• Hint: sscanf(str, "g x%hx", &var) first looks for a g and x; if it
finds them, then it reads in a short hex integer into var. The sscanf
call returns the number of values it successfully read (in this case, 1 if it
manages to read a value into var; 0 otherwise).
• Ignore Text After The Command: Ignore any text (whitespace or nonwhitespace) that appears after the command but before the end of the line.
E.g., s r6 xa0 This sets r6 to 160\n is legal.
G. Simulating The LC-3 Architecture
• As in Lab 5, the LC-3 CPU should be modeled as a value of type struct
CPU, allocated in the main program. Routines that need the CPU should be
passed a pointer to the CPU value.
• The CPU’s memory should be represented as an array of Word values, indexed
by Address values. (These are typedef defined types.)
• A Word is a short int, 16 bits on the alpha machine. Since Word values
are signed, 0x8000 – 0xffff represent -32,768 to -1.
• An Address is an unsigned short int (compare with the unsigned
char used for the SDC). Since unsigned values are never negative, as
Address values, 0x8000 – 0xffff represent 32,768 to 65,535.
• You may need a cast to convert between Word, Address, and integer values.
For example, if Word w = 0x8000, then (Address) w + 1 == 0x8001, but
w + 1 == -32767.
• If you don't already know it, look up the difference between %x and %hx in
printf formats.
H. Executing LC-3 Instructions
• If you execute an instruction at xFFFF, then incrementing the PC should wrap
it around to x0000. Note: Patt & Patel's LC3 simulator causes an error if you
execute the instruction at location xFFFF. Our default value at xFFFF is
x0000, which means NOP.
CS 350: Comp Org & Asm Pgm’g
–4 –
© James Sasaki, 2015
Illinois Institute of Technology
Lab 8 and Final Project
• Executing the RTI instruction (Return From Interrupt, opcode 8) or the
unused opcode 13 should print an error message but continue execution. (Patt
& Patel's LC-3 simulator behaves differently.)
• Implement traps x20, x21, x22, x23, and x25. For any other trap vector
(including x24: PUTSP), print an error message and halt execution (set the
running flag to false).
• Execute each TRAP command in one instruction cycle. (Don’t simulate the I/O
registers.) E.g., executing PUTS should print out the entire string pointed to
by R0.
• For the IN and GETC traps, the user should enter a \n after the character to
be read in. If the user just enters \n without a preceding character, then use
\n as the character read in.
• Technically, the OUT trap is only supposed to print the right byte of R0, but
if your OUT only works if the left byte of R0 is x00 or xff, that's okay.
Similarly, the IN and GETC traps are supposed to overwrite only the right
byte of R0 and leave the left byte unchanged, but if you set the left byte to
x00 or xff, that's okay too. [Updated 5/2]
• Because the simulator prints out a trace of execution, printing a prompt and
doing a read (using PUTS and GETC) doesn’t behave exactly like it does with
Patt & Patel’s simulator: You have to wait until the GETC executes and asks
for your input before actually typing in the character.
I. Code Framework
• You should be able to adapt your Lab 5 SDC simulator to do the final project.
• Feel free to use standard library functions like strcmp. (Don't forget to
#include <string.h>.)
• Remember, your program gets tested on alpha.cs.iit.edu, so do not use
libraries available only on one operating system, like conio.
• The method for representing the condition code is up to you: an integer is fine,
but it could also be the rightmost three bits of an unsigned char, for
example. Or literally the character 'N', "Z', or 'P'.
CS 350: Comp Org & Asm Pgm’g
–5 –
© James Sasaki, 2015
Illinois Institute of Technology
Lab 8 and Final Project
• Also feel free to add more fields or named constants to your program, such as
#define CC_ZERO … to represent condition code zero.
J. Lab 8: Partial Final Project
• For Lab 8, submit a version of the project that reads the *.hex file and
supports the h, d and q simulator commands. I.e., you should be able to load
a program into memory, initialize the control unit (registers, PC, condition
code, and running flag) and dump them out on request. (And print out the
help message.)
• If you want to recognize other commands (goto, set memory/register, and
execute) and print a message saying they aren't implemented yet, that's fine
but not required.
• Collaboration: You can work with others on Lab 8. However, you may not use
code from previous semesters.
• Lab 8 is due Fri Apr 17. You get an extension to Mon Apr 20 if you attend
lab on Nov 17 or 18.
K. The Final Project
• The final project is due on Sat May 2. (No extensions for attending labs.)
You can submit multiple copies of your final project onto Blackboard. Each
time you submit, submit the whole project — I’ll grade your last submission
(and throw away the earlier ones).
• Collaboration: You can't work with others on the remaining part of the
project (the goto, set, and execution commands).
• Late penalty: Turn in on Sun May 3 for 5% off. No submissions after that.
L. Sample Solutions
• Working simulators will be available on alpha in ~sasaki/Lab8_soln and
FP_soln. Sample *.hex files and sample runs of them will also be posted.
M. Grading Scheme
• A detailed grading scheme will be published: A significant part of the grade
requires that the code be commented, readable, and have good-sized, cohesive
CS 350: Comp Org & Asm Pgm’g
–6 –
© James Sasaki, 2015
Illinois Institute of Technology
Lab 8 and Final Project
routines. (Don’t have just a main program hundreds of lines long. Each
routine should do one conceptual thing.)
CS 350: Comp Org & Asm Pgm’g
–7 –
© James Sasaki, 2015