Starting from:

$25

CS2110 - Homework 4 - LC-3 Datapath - Solved

1           Overview
1.1         Purpose
The purpose of this assignment is for you to understand the datapath and the state machine of the LC3 processor (as presented in the Patt textbook, and Lecture and Lab). You will build several subcircuit components, using Circuitsim, to complete the LC-3 datapath circuit. Then you will complete the microcode for multiple LC-3 machine instructions. The microcode controls the finite state machine, and each machine instruction includes one or more states.

Note: The LC-3 implementation in this assignment is simplified, and does not include all the features or components of the full LC-3 from the Patt textbook or Lecture or Lab.

1.2         Task
Please read this entire document before starting. It includes many details of the assignment, as well as tips, tricks, and references that will help you.

•   First, in Part 1, you will complete several circuits in the LC-3 datapath, including the ALU, the PC subcircuit, and the Condition Code (CC) logic subcircuit. You will build these circuits in the LC3.sim file. You may use the manual LC-3 to debug and test your subcircuits.

•   Next, in Part 2, you will complete the microcode for many of the LC-3 machine code instructions, entering data in the microcode.xlsx spreadsheet we have provided. You may test your microcode, using the ROM.dat file and importing data into Fsm subcircuit of the LC3.sim circuitsim file.

Hello and welcome to Homework 4! In this homework, you’ll develop familiarity with the functionality and implementation of the LC-3 Datapath that we’ve covered in class. You’ll be responsible for building in three missing pieces of the LC-3 hardware which were removed as well as writing the microcode for the bulk of LC-3 instructions.



2.1         The LC-3’s Microcontroller
The LC-3 datapath we’ve discussed in class contains a lot of pieces very similar to circuits we’ve seen or even made before (e.g. an ALU, a register file with 8 edge-triggered general purpose registers, a RAM unit, etc.). One piece we’ve mostly referred to as a “black-box” in the past is the microcontroller. It’s responsible for controlling the entire datapath, and getting it to properly execute the instructions that we give it. That’s a big task! So how does the microcontroller actually work? In this homework, we’ll build a few datapath components to develop some familiarity with the LC-3, and then we’ll actually write the “microcode” which allows the microcontroller to function.

 

The microcontroller, shown above, is a finite state machine. It has 59 possible states (holy dancing crab!), and because it is implemented in the “binary reduced” style, it needs 6 bits to store all its possible states. It also has 49 output bits of output flags, including 10 which are used to determine the next state and 39 which extend throughout the datapath to control other pieces of the LC-3. That would be a lot of very complex hardware—if it were built entirely in hardware.

It turns out there is an easier way. We can actually use a ROM (Read-only Memory) in order to specify the behavior of each distinct state in the state machine (e.g. each instruction will map to a series of entries in the ROM. Each entry in the ROM represents something called a micro-state, which is an individual state of the finite state machine and a potentially a component of a slightly larger sequence of states known as a macro-state. FETCH is a macro-state, and each of the execute stages of LC-3 instructions is also a macro-state. Each of the macro-states will require between 1-5 micro-states to complete, depending on the complexity of the instruction.

What does a ROM entry look like? We encourage you to go ahead and open up microcode.xlsx, on the microcode sheet, to follow along.

 

A ROM entry is basically a long binary string. The last few bits of it cover the transition to the next state—you don’t need to worry about this at all during this homework, so we’ve covered it in dark grey on the right. Do NOT modify the NEXT bits, or stuff will break. Each of the other bits corresponds to a signal asserted onto the datapath during that clock cycle. For this homework, we only require that you implement 19 of the signals asserted onto the datapath (notice that three of these are 2-bit signals).

We’ve also simplified and removed a number of micro-states which aren’t directly a part of the LC-3’s main instructions. There are only 36 micro-states in this homework. We’ve also given you the microcode for some of these micro-states. Your task is to fill in the rest and finish the LC-3 microcontroller!

2.2         Files Provided
•   LC3.sim - a large CircuitSim file containing the LC-3 AND a “Manual LC-3” which does not need to be modified in any way but is simply present as a tool for you while writing microcode. You should only modify the CC-logic, PC, and ALU subcircuits in this file.

•   microcode.xlsx - an Excel document in which you will write your microcode. Do not touch cells that have been blacked out and do not edit the output sheet directly

•   ROM.dat - a text file which you can paste your microcode into and then import into the LC-3.

•   tests/ - a subdirectory which contains a number of test cases you can use to verify the functionality of your circuit and microcode.



•   LC-3InstructionsDetail.pdf - a PDF with descriptions and pseudocode for each instruction.

2.3         Tasks You Must Complete
1.    Implement the ALU, PC, and CC-Logic subcircuits in LC3.sim.

2.    Complete the microcode in microcode.xlsx, in the microcode sheet.

3           Part 1: Implementation
3.1         Completing the CircuitSim File
The LC-3 datapth you will be working on for this homework, as contained in the LC3.sim file, is a complete but simpler version of the LC-3 datapth you might encounter in your textbook’s appendix C. This is due to the lack of IO related components in the LC-3 datapath that you will be working on as these components and how they are utilized are beyond the scope of this particular homework, though they will be encountered in a later part of this course. As such, you can ignore the components not present in the LC3.sim file as they appear in the textbook.

3.1.1         ALU
You will need to build the ALU (Arithmetic Logic Unit) subcircuit in LC3.sim.

The ALU will perform one of 4 functions and Output it depending on the ALUK signal:

1.    ALUK = 0b00: A + B

2.    ALUK = 0b01: A & B

3.    ALUK = 0b10: NOT A

4.    ALUK = 0b11: PASS A

You should be able to use what you learned from HW3 to populate this subcircuit easily.

3.1.2         PC
You will need to build the PC (Program Counter) subcircuit in LC3.sim.

The PC is a 16 bit register that holds the address of the next instruction to be executed. During the FETCH stage, the contents of the PC are loaded into the memory address register (MAR), and the PC is updated with the address of the next instruction. There are three scenarios for updating the PC:

1.    The contents of the PC are incremented by 1. Selected when PCMUX = 0b00.

2.    The result of the ADDR (an address-adding unit) is the address of the next instruction. The outputfrom the ADDR should be stored in the PC. This occurs if we use the branching instruction, (BR). Selected when PCMUX = 0b01.

3.    The value on the BUS is the address of the next instruction. The value on the BUS should be storedinto the PC. An example of this functionality is the JMP instruction.

Selected when PCMUX = 0b10.

The PC should only be loaded on a rising clock edge when the LD.PC signal is on.

Ensure that you don’t reach the unused case (PCMUX = 0b11) of the circuit, or else spooky stuff might happen (undefined behavior in LC-3).

3.1.3         CC-Logic
The LC-3 has three condition codes: N (negative), Z (zero), and P (positive). These codes are saved on the next rising edge after the LC-3 executes Operate or Load instructions that include loading a result into a general purpose register, such as ADD and LDR.

For example, if ADD R2, R0, R1 results in a positive number, then NZP = 001.

The LC-3 appendix on canvas should help determine which instructions set the Condition Code.(See page 5 pf PattPatelAppA.pdf)

The CC subcircuit should set N to 1 if the input is negative, set Z to 1 if the input is zero, and set P to 1 if the input is positive. Only 1 bit in NZP should be set at any given time. Zero is not considered a positive number.

Bit 2 (the MSB) is N, Bit 1 is Z, Bit 0 is P.

With that in mind, set the correct bit and implement this circuit in the CC-Logic subcircuit.

Implement this circuit in the subcircuit CC-Logic.

Hint: you can use a comparator for this subcircuit! They are found in the Arithmetic tab in CircuitSim.

4           Part 2: Writing the Microcode
•   Now that you’ve developed some familiarity with the datapath ( and perhaps tried to “act as the microcontroller” to execute instructions on the Manual LC-3 subcircuit - see the “checking your work” section for information on this), you can complete the final part of the homework: writing the microcode for a number of micro-instructions on the LC-3!

•   In microcode.xlsx Excel document, microcode sheet, there exist a number of macro-states. Among them are FETCH and the execute stages for most instructions supported by the LC-3 (we’ve removed several macro-states related to trap and interrupt handling). For each macro-state, we’ve provided space for the micro-states which will make up that macro-state, and for each micro-state, we’ve handled all of the logic related to transitioning to the next micro-state. We’ve also implemented a small subset of the macro-states in order to provide inspiration to you, our students (you’re welcome).

•   You should complete all the remaining macro-states by filling in their micro-states.

– If you notice that the output column to the right of the blacked-out columns (column AH) is showing artifacts like #NAME?, try opening the Excel spreadsheet in your Georgia Tech Office 365 Online Excel workspace. Sign in to Office 365 with Georgia Tech credentials, select ‘Excel’, and then choose ‘Upload and open’ to edit the excel sheet online.

4.1         How to Test your Microcode
At any time that you want to test your microcode, you can export it from the .xlsx file and apply it to LC-3 hardware by following these steps. IMPORTANT NOTE: Passing all of the tests provided does not guarantee that you have a functional datapath, any number of coincidences could cause you to get the correct output with incorrect functionality. As always, we reserve the right to grade with additional test cases.

1.       Go to the output sheet of microcode.xlsx

2.       Copy all of column D from row 1 through row 64

3.       Paste the result into ROM.dat

4.       Ensure that ROM.dat is in the same directory as ./cs2110docker.sh (or some subdirectory)

5.       In Docker CircuitSim, open LC3.sim. Navigate to the ‘Fsm’ subcircuit. This circuit contains themicrocontroller.

6.       Right-click on the ROM and select “Edit contents.”

7.       Select “Load from file”

8.       navigate to and select “ROM.dat.” This will load the ROM.

9.       Navigate to the ‘LC-3’ subcircuit.

10.   You can now load a program into the RAM, following the instructions above in the Manual LC-3 sections.

11.   To run the LC-3, you can manually click through the CLK signal or use ‘Ctrl-K’ to start or stop theautomatic clock. After your program has stopped executing (you can tell when it’s finished running because it will HALT and the datapath will stop changing).

12.   Tests inside the tests/ directory have a comment at the end of the .asm file which explains the system state after the end of the program’s execution. To test whether the program acted correctly, go to the ‘LC-3’ circuit and double-click into the ‘REG FILE’ element that is placed in the datapath. Note: you should not just click into the ‘REG FILE’ subcircuit, as this will not properly load the state of the specific ‘REG FILE’ element that’s built into the LC-3, just some generic REG FILE.

5           Checking Your Work
Once complete, run the autograder

java -jar hw4-tester.jar

inside the Docker container in the command prompt in the same directory as your LC3.sim file.

If all of the relevant tests pass, you’ve completed this part of the homework. Congrats! This autograder will check your microcode file against your own LC3.sim file, so ensure you finish work on the LC3 subcircuits from part 1 before beginning part 2

If all of the relevant tests do not pass, and you would like to double check your own understanding of how your microcode interacts with the LC-3 Datapath, you can use the “Manual LC-3” subcircuit provided in the homework file. NOTE: This is an ungraded part of the homework that you do not need to use to get full points on this homework. This is supposed to be used as a debugging tool to help you understand how to complete the homework.

5.1         Using the Manual LC-3
•   Once you have your PC, CC-Logic, and ALU complete you can begin working on the meat of this homework, understanding how LC-3 works.

•   In lecture and lab we have covered the signals in the datapath and how they are used when tracing an instruction.

•   The first thing you will want to do is use the Custom-Bus, GateBUS, and LD.IR signals to set a custom IR value on the next rising edge. Until you figure out the states for fetch, you can use these steps to set your IR with any instruction you want to work on.

•   Once you have an idea of how fetch works, you can load some instruction(s) in the RAM. In order to do that:

1.    right-click the RAM near the bottom of the Manual LC-3 circuit

2.    select “edit contents”

3.    click “Load from file”

4.    locate and select one of the provided test files in the homework (ex: addand.dat)

5.    close the edit contents menu

•   Now that you have loaded the RAM with a program, you can fetch instructions into the IR.

•   Now that you have an instruction in the IR, you can start executing it. In order to do that you can turn on the different signal pins on the right in order to control the datapath and move data around like we did in lecture/lab. Once you think you know how an instruction is executed, you can enter it into the microcode spreadsheet, the process for which is outlined below.

–    Tests inside the tests/ directory have a comment at the end of the .asm file which explains the system state after the end of the program’s execution. You must ensure that this is the system state after you have run every instruction sequentially through the simulator.

–    To test whether the program acted correctly, go to the ‘LC-3’ circuit and double-click into the ‘REG FILE’ element that is placed in the datapath. Note: you should not just click into the ‘REG FILE’ subcircuit, as this will not properly load the state of the specific ‘REG FILE’ element that’s built into the LC-3, just some generic REG FILE.

–    Bonus tip: Use Ctrl-R to reset the simulator state and easily clear RAM and registers to 0 in order to test again.

•   If you are familiar with LC-3 assembly (you will learn it over the course of the next two weeks), you are welcome to write your own test programs to verify your code. Make sure that your programs do not start at x3000, as in this homework only we will start execution at x0000 for simplicity’s sake. You can compile LC-3 assembly projects to machine code by following the LC-3 ISA, and then create your own RAM.dat files. We can’t guarantee that we’ll be able to help with these test cases in office hours, though.

NOTE: The above section on the manual LC-3 is not needed to get full points on this homework. This is supposed to be used as only a debugging tool to help you understand how to complete the homework.

5.1.1         Tips, Tricks, Resources
This can all be pretty daunting to read and understand at first. But it’s not the end of the world so do not panic, carry a towel and make sure to use the resources available to you. Here are some options:

•   LC-3 Datapath Diagram and ISA Quick Sheet: Canvas → Files → LC-3 Resources → LC3ReferenceSheet.pdf

•   LC-3 Instructions Detail Sheet: LC-3InstructionsDetail.pdf, in this homework.zip

•   The Manual LC-3!

•   Your friendly TAs (Office Hours, Piazza, etc.)

•   Textbook

•   Appendix on Datapath Control Signals in this PDF.

6           Setup
The software you will be using for this project and all future circuit based assignments is called CircuitSim - an interactive circuit simulation package.

In order to use CircuitSim, you must have Docker up and running. If you do not have Docker, follow the instructions laid out in the installation guide found under Canvas → Files → Docker. Make sure you are using the updated docker container!! The docker-script should automatically do this when you run it. Check by ensuring that the version of CircuitSim in the docker container is version

1.8.2

CircuitSim comes pre-installed on the Docker image, and should be accessible via the desktop. Please only use the CircuitSim through Docker to build your circuits as it is the correct version. CircuitSim downloaded from elsewhere may not be compatible with our grader. You have been warned.

CircuitSim is a powerful simulation tool designed for educational use. This gives it the advantage of being a little more forgiving than some of the more commercial simulators. However, it still requires some time and effort to be able to use the program efficiently.

Please do not move or rename any of the provided circuits/elements. You should only be modifying the contents of the ALU, PC, and CC-Logic. The only things you need to modify outside of that is RAM/ROM contents and using the pins/buttons provided

More products