Final Project: LC-3 Simulator

Similar documents
Lab 5: SDC Virtual Machine

CS102: Standard I/O. %<flag(s)><width><precision><size>conversion-code

CS354 gdb Tutorial Written by Chris Feilbach

Outline. Computer programming. Debugging. What is it. Debugging. Hints. Debugging

CS 1550 Project 3: File Systems Directories Due: Sunday, July 22, 2012, 11:59pm Completed Due: Sunday, July 29, 2012, 11:59pm

CpSc 1111 Lab 4 Part a Flow Control, Branching, and Formatting

ENCM 339 Fall 2017: Editing and Running Programs in the Lab

Lab 9: Pointers and arrays

Homework # 7 DUE: 11:59pm November 15, 2002 NO EXTENSIONS WILL BE GIVEN

CSE 351, Spring 2010 Lab 7: Writing a Dynamic Storage Allocator Due: Thursday May 27, 11:59PM

CS 1110 SPRING 2016: GETTING STARTED (Jan 27-28) First Name: Last Name: NetID:

EE 355 Lab 4 - Party Like A Char Star

Project 4: File System Implementation 1

int $0x32 // call interrupt number 50

CS1110 Lab 1 (Jan 27-28, 2015)

2 Steps Building the Coding and Testing Infrastructure

CSE 361 Fall 2017 Lab Assignment L2: Defusing a Binary Bomb Assigned: Wednesday Sept. 20 Due: Wednesday Oct. 04 at 11:59 pm

Project #1 Exceptions and Simple System Calls

3. A Periodic Alarm: intdate.c & sigsend.c

Eat (we provide) link. Eater. Goal: Eater(Self) == Self()

1. Allowed you to see the value of one or more variables, or 2. Indicated where you were in the execution of a program

Starting to Program in C++ (Basics & I/O)

GDB Tutorial. A Walkthrough with Examples. CMSC Spring Last modified March 22, GDB Tutorial

UNIVERSITY OF NEBRASKA AT OMAHA Computer Science 4500/8506 Operating Systems Summer 2016 Programming Assignment 1 Introduction The purpose of this

Midterm 2 Review Chapters 4-16 LC-3

Recitation #11 Malloc Lab. November 7th, 2017

Printable View of: Week 13: Miscelaneous cool features. Returns from standard functions. returns from standard functions: scanf(), fopen()

Intro. Scheme Basics. scm> 5 5. scm>

CIS 2107 Computer Systems and Low-Level Programming Fall 2011 Midterm Solutions

Syntax of LC-3 assembly: Language elements. Instructions

This section covers the MIPS instruction set.

Malloc Lab & Midterm Solutions. Recitation 11: Tuesday: 11/08/2016

Embedded Controller Programming 2

ECE 206, Fall 2001: Lab 3

CpSc 1111 Lab 9 2-D Arrays

CpSc 1011 Lab 4 Formatting and Flow Control Windchill Temps

CS 392/681 Lab 6 Experiencing Buffer Overflows and Format String Vulnerabilities

CS 103 Lab - Party Like A Char Star

CMSC162 Intro to Algorithmic Design II Blaheta. Lab March 2019

CSE 351. GDB Introduction

Project C: B+Tree. This project may be done in groups of up to three people.

Project #1: Tracing, System Calls, and Processes

CS 222: Pointers and Manual Memory Management

CS52 - Assignment 8. Due Friday 4/15 at 5:00pm.

ENCM 335 Fall 2018 Lab 2 for the Week of September 24

Lab 03 - x86-64: atoi

COMP 3500 Introduction to Operating Systems Project 5 Virtual Memory Manager

Programming Assignment Multi-Threading and Debugging 2

Assignment 5: Priority Queue

CS 4410, Fall 2017 Project 1: My First Shell Assigned: August 27, 2017 Due: Monday, September 11:59PM

AN INTRODUCTION PROGRAMMING. Simon Long

EECS150 Lab Lecture 5 Introduction to the Project

Jackson State University Department of Computer Science CSC / Advanced Information Security Spring 2013 Lab Project # 5

High Performance Computing

CpSc 1011 Lab 3 Integer Variables, Mathematical Operations, & Redirection

CpSc 1111 Lab 4 Formatting and Flow Control

UNIVERSITY OF NEBRASKA AT OMAHA Computer Science 4500/8506 Operating Systems Fall Programming Assignment 1 (updated 9/16/2017)

Lecture 12 CSE July Today we ll cover the things that you still don t know that you need to know in order to do the assignment.

Customizing DAZ Studio

CS155: Computer Security Spring Project #1

CS61C Machine Structures. Lecture 4 C Pointers and Arrays. 1/25/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

CS168 Programming Assignment 2: IP over UDP

Standard C Library Functions

15-213, Spring 2008 Lab Assignment L1: Manipulating Bits Assigned: Jan. 15, Due: Wed., Jan. 30, 11:59PM

HW1 due Monday by 9:30am Assignment online, submission details to come

CS 220: Introduction to Parallel Computing. Beginning C. Lecture 2

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

ECE 353 Lab 3. (A) To gain further practice in writing C programs, this time of a more advanced nature than seen before.

Floating-point lab deadline moved until Wednesday Today: characters, strings, scanf Characters, strings, scanf questions clicker questions

Programming Standards: You must conform to good programming/documentation standards. Some specifics:

Debugging uclinux on Coldfire

It is academic misconduct to share your work with others in any form including posting it on publicly accessible web sites, such as GitHub.

Agenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011

Homework 2 - KW26 - using 40 hexadecimal digits

CpSc 1111 Lab 5 Formatting and Flow Control

Laboratory Assignment #3. Extending scull, a char pseudo-device. Summary: Objectives: Tasks:

CMSC 201 Spring 2017 Project 1 Number Classifier

Gold Hw8 Problem 3: TT Securities, Incorporated

COSC 2P91. Introduction Part Deux. Week 1b. Brock University. Brock University (Week 1b) Introduction Part Deux 1 / 14

CS61C : Machine Structures

We first learn one useful option of gcc. Copy the following C source file to your

Ch. 11: References & the Copy-Constructor. - continued -

CS106 Lab 1: Getting started with Python, Linux, and Canopy. A. Using the interpreter as a fancy calculator

CS4023 Week06 Lab Exercise

CS 61c: Great Ideas in Computer Architecture

Final Programming Project

CS311 Lecture: The Architecture of a Simple Computer

Subroutines & Traps. Announcements. New due date for assignment 2 5pm Wednesday, 5May

Strings. Steven R. Bagley

CS 61C: Great Ideas in Computer Architecture. Lecture 2: Numbers & C Language. Krste Asanović & Randy Katz

Chapter 7 Assembly Language. ACKNOWLEDGEMENT: This lecture uses slides prepared by Gregory T. Byrd, North Carolina State University

Week 3. This is CS50. Harvard University. Fall Cheng Gong

Computer Science 322 Operating Systems Mount Holyoke College Spring Topic Notes: C and Unix Overview

CS 220: Introduction to Parallel Computing. Input/Output. Lecture 7

A heap, a stack, a bottle and a rack. Johan Montelius HT2017

A practicalintroduction to embedded programming. Brian Plancher 10/17/2018

EECE.2160: ECE Application Programming

Agenda. CS 61C: Great Ideas in Computer Architecture. Lecture 2: Numbers & C Language 8/29/17. Recap: Binary Number Conversion

HOT-Compilation: Garbage Collection

CS 103 Lab 6 - Party Like A Char Star

Transcription:

Final Project: LC-3 Simulator Due Date: Friday 4/27/2018 11:59PM; No late handins This is the final project for this course. It is a simulator for LC-3 computer from the Patt and Patel book. As you work on this project, we ll also be learning about the LC-3 architecture and how to write LC-3 assembly programs in lecture and labs. What we ll end up with here is a line-oriented command-line program that reads in initial memory contents from a file (in hexadecimal format) and then lets the user enter commands to execute the program s instructions and inspect registers and memory (much like an interactive debugger such as gdb). You ll also be implementing the code that executes instructions on the LC-3. After completing this project, you ll not only be able to understand how to implement a basic digital computer in software, but also how to implement a read-eval-print-loop (REPL)-based interpreter, like you are probably familiar with if you program with Python or Ruby (or other high-level scripting languages). Your goal is to fill in all of the missing functionality in lc3.c which is indicated by FILL ME IN comments. You should add your own code in these locations. Please do not hesitate to ask for help; this is a challenging project. You should be sure to get started early. Simulating the LC-3 The LC-3 CPU should be modeled as a struct with type cpu_t, allocated in the main program. Routines that need the CPU should be passed a pointer to the CPU value. A word_t is typedef ed to be a short int, 16 bits on fourier. Since word_ts are signed, 0x8000-0xffff represent -32768 to -1. An address_t is an unsigned short int. Make sure to read the comments on modeling instruction formats with structs in C in lc3.h. This is a fairly advanced usage of C structs and may take some time to wrap your head around. The memory of the machine is represented as an array of word_t values indexed by address_t. Registers are represented as an array of word_t values indexed by integers (0-7 since there are 8 registers). The PC is an address_t; the IR is a word_t. Since unsigned values are never negative, as address_t values, 0x8000-0xffff represent 32768 to 65535. You may need a cast to convert between word_t, address_t, and integer values. E.g., as a word_t, 0xffff is -1, but as an address_t, 0xffff is 65535. A base register contains a word_t; to use its value as an address, you need to cast it: (address_t)(word_val + offset). 1

Loops of the form for every address_t, do... are a little tricky. If i is an address_t variable, then for (i = 0; i < 65536; i++){...} will cause an infinite loop because an address_t is always < 65536. You can check for i == 65535 (in which case you re one iteration short), or you can check for the second time that i == 0 (which is a little tricky), or you can make i not be an address_t. Coding Assignment (100 points total) The input file should be a command line parameter. If it s omitted, your program should produce an error. 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). An sscanf format of %x will read in an unsigned integer, which you can cast to a word_t to store in memory. 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. No errors are necessary here. 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: In general, the LC-3 text editor can translate hex files to executable object code via Translate Convert Base 16, but it won t handle a hex file with comments added to it. All other memory locations should be set to zero. In particular, don t simulate the TRAP table in low memory (x00 - xff) or the trap-handling code in upper memory. Once the program is read into memory, initialize the PC to the.orig value, the IR to zero, the condition code to Z, and the running flag to 1, then dump the CPU and memory. Once the.hex file is loaded into memory, a prompt will be displayed to start the command loop. The user should enter a carriage return after each command (h\n, for example). The possible commands include?, h, d, q, N, and an empty line, where N is some integer. You must be careful with whitespace. You can redirect your program s output to a file, do the same with the solution, and use the diff command to spot differences that aren t obvious to the eye. Differences from the Patt & Patel Simulator Your simulator should produce the same results as the Patt & Patel simulator with some slight differences. If you execute an instruction at xfff then incrementing the PC should wrap it around to x0000. (Patt & Patel s simulator causes an error if you execute the instruction at that location.) By default, for us, M[xFFFF] = 0 is a NOP. 2

Executing the RTI instruction (Return from Interrupt, opcode 8) or the unused opcode 13 will print an error message but continue execution. (Patt & Patel s LC-3 simulator behaves differently.) Only traps x20, x21, x22, x23, and x25 need to be implemented. For any other trap vector (including x24, PUTSP), an error message will be printed and execution will halt. A TRAP command will be executed in one instruction cycle. Their simulator actually simulates I/O register interactions. Because of this, we say that we are emulating I/O, rather than simulating. 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. 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. Code Framework Feel free to use the standard library functions like strcmp. (Don t forget to #include <string.h>.) Remember, your program gets tested on fourier, so don t use libraries like conio that aren t available there. Get the lc3 project code by logging into fourier and running the follow- Getting the Code ing: $> cp /home/khale/handout/final.tgz. $> tar xvzf final.tgz final/ final/tests/ final/tests/t1.exp final/tests/t3.exp final/tests/t8.hex final/tests/t6.exp final/tests/t5.exp final/tests/t1.hex final/tests/t7.hex final/tests/t2.exp final/tests/t8.exp final/tests/t3.hex final/tests/t2.hex final/tests/t0.exp final/tests/t4.hex final/tests/t4.exp final/tests/t10.exp 3

final/tests/t7.exp final/tests/t9.exp final/tests/t5.hex final/tests/t6.hex final/test_harness final/makefile final/lc3.h final/lc3.c final/lc3-soln $> cd final $> ls lc3.c lc3.h lc3-soln Makefile test_harness tests $> make gcc -Wall -Wno-unused-function -Wno-unused-variable -Wno-unused-but-set-v -Wno-packed-bitfield-compat -lm -o lc3 lc3.c If you run the skeleton code you ll see the following: $>./lc3 CS 350 Final Project: LC-3 Simulator Usage:./lc3 <.hex file> Your job here is to fill in the functions in lc3.c which have FILL ME IN comments written in them. See the comments in the code for further instructions. I ve also included a test harness for you to test your code. You can run it like so: $> make test # Testing lc3... Test 0 - [FAIL] Test 1 - [FAIL] Test 2 - [FAIL] Test 3 - [FAIL] Test 4 - [FAIL] Test 5 - [FAIL] Test 6 - [FAIL] Test 7 - [FAIL] Test 8 - [FAIL] Test 9 - [FAIL] Test 10 - [FAIL] 0 out of 11 test cases passed Notice that the code will initially fail all the test cases or hang. Your job is to make it pass them all. These are example.hex files, and I ve included them in a separate directory named tests. Notice that if you add more.hex files in the tests directory this time, you will break the test harness. This is because there is something special going on in order to test your program interactively. For grading, we will be using other tests in addition to the ones in this directory, so make sure you pass all the tests we provide. A good strategy here to get started is to start with the main() function and work your way down based on the functions that are called from there. 4

Programming Notes You re welcome to add functions that aren t already present in the skeleton (e.g. helper functions). To read in a line of text and see what s in it, we can t use scanf because it ignores line ends. The skeleton uses fgets to read a line from the data file (as a string of characters) into a buffer; then it uses sscanf to do a formatted read of the buffer. First, the skeleton uses fgets to read a line of text from the data file into a buffer. fgets returns a pointer to char. If the pointer equals NULL, we hit end-of-file. (If it successfully reads data, fgets returns a pointer to the buffer you used.) The usual case is that fgets copies characters from the file into the buffer until it sees the end of the line ( \n ). (It does copy the \n into the buffer.) There s also a safety feature: We give fgets the length of the buffer; if fgets reaches the end of the buffer before seeing the \n, it stops, so as not to overrun the end of the buffer. This is a safety feature because a standard way to attack a program is to fill memory with evil code by writing way past the end of a buffer. After fgets reads characters into the buffer, we can use sscanf to read data from the buffer. Instead of scanf(format, &var1,...), which reads data from standard input, we use sscanf(string, format, &var1,...) to read data from the string. Like scanf, sscanf returns the number of items that that particular call managed to read, so we can tell whether or not the read found everything. E.g., x=sscanf(buffer, "%d %d", &y, &z); tries to read two integers from the string buffer into variables y and z. It sets x to 2,1, or 0, depending on whether it sets both y and z or just y or neither y nor z. Note: Just because scanf or sscanf doesn t find what it s looking for (e.g., by not finding an integer when reading with %d), that doesn t always mean you hit end-of-file or end-of-string; it might be that there was more input but it just didn t look like an integer. Extra Credit Opportunities There are several possibilities for extra credit. They will involve some ambitious extension to the project or something related to it. The credit earned for such projects will vary, but can be significant for the more ambitious ones. It s really more for the fun of it. Come see me if any of these sound exciting to you. Extend your simulator to actually simulate I/O interactions at the register level (like the Patt & Patel simulator does.) Design the hardware for a floating point unit for LC-3 and add appropriate ISA extensions for it. Design the hardware to make the LC-3 virtualizable. Pick a one-instruction-set (OISC) computer of your choice and write an assembler that translates from LC-3 assembly to that OISC assembly. Some people would call such a tool a trans-piler. 5

Write an x86 LC-3 assembler. Implement LC-3 as a breadboard-based computer. Write a custom operating system for the LC-3. Add virtual memory support to the LC-3. This would, in theory, allow you to port a real-life OS like Linux to LC-3. Learn a hardware description language such as Verilog or VHDL and write an implementation of the LC-3. If you are really ambitious, and would like to synthesize your design (going from an HDL to a transistor based design using automated tools), I might even pay for you to have your chip design taped out (fabricated). It must, of course, work! Make a web-based LC-3 simulator, e.g. using Javascript, HTML5 canvases, or whatever hot web language the kids are using these days. This is something that could be used in later instantiations of this course! Hand-in Instructions Make sure to put your name on your submission. Submissions without names will be given zero points! For code, this means put a comment at the top of your C file with your name on it. For the code, you must hand it in digitally. Once you re happy with your code, in the directory where your code is (final), run the following on fourier: $> make handin Late handins NO LATE HANDINS FOR THIS ASSIGNMENT! Programming Notes lc3.c contains the skeleton code for this lab. Remember, sscanf returns the number of items it was able to read. E.g., x=sscanf(s, "%d", &y); tries to read an integer from a string s into a variable y. If it succeeds, x is set to 1, else x is set to EOF (which you should not assume will be zero). Note that this time your program is an interactive program. We are testing your program by recording interactions with the instructor solution, and making sure that your program behaves the same way. If you re curious how we do this, lookup the expect program. Pretty nifty. If, when you run make test, your program hangs, it is because expect does not understand your program s output and is waiting for the correct output. If this happens, invoke expect manually with expect -f tests/tn.exp./lc3 where N is the test number. Spot where the output stops with expect and look back to the previous input that expect typed at your prompt. The difference between the solution and your program will lie somewhere between those two points. 6

LC-3 ISA Reference 7

Note here that we will be ignoring the RTI instruction. 8