Lab 5: Decoding AVR Instructions

After reviewing the actual encoding used in the AVR processors, I worked up a spreadsheet that includes enough instructions to build some simple programs for our simulator. The encoding is not so complicated that we really need to create our own encoding, so we will use the real chip encoding scheme. This will allow us ot use the actual AVR assembler to generate sample programs for our simulator projects.


This lab is to be worked on in your group. I want each team member to submit the final code in the class repository, but you can freely share code within your group.

Basic Decoding Logic

The decoder receives a 16-bit instruction from the fetch unit. That instruction may be augmented by a second 16-bit constant that we will ignore for now.

Here are the input signals for this unit:

  • IR1: - 16-bit instruction code

Here are the output signals to generate

  • INST: binary instruction code (numbered alphabetically
  • RegD: 8-bit destination register (if used)
  • RegR: 8-bit source register if needed
  • Const: 16-bit Constant (extended from smaller values if needed)
  • IR2: just set this up as a boolean indicating we need another chunk of data. Use the lecture notes on decoding to find the pattern for this code.

Your Job

This is a bit messy, but fairly simple.

All you need to do is write a simple function that takes in the 16-bit instruction opcode and sets global variables for each of the output values. You should break up the input opcode into chunks, and use a switch statement to classify each code. The leading four bits will be the first chunk to test. Once you have the right group, an inner case statement can help decode the rest of the bits.

You will want to use the C++ bitset class for the decoding work.

Test Your Function

You should set up a catch.hpp test file to check your function. You can feed it sample data for each instruction, derived from the example spreadsheet provided in the lecture notes. You do not need to test with all possible registers, just a few should be fine.

Divide up the work among your team members. This code will end up in the actual simulator decode unit once we get that stage set up.