Exam 1 Lab

In building a simulator we will need to break up a fixed size instruction code obtained by the fetch step into various fields. In this exercise you will develop prototype code for the decoder.

We will be using data types provied by the standard C++ library cstdint for this problem.

Decoder Class

Set up a simple C++ class named Decoder, with this specification:

Decoder.h
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
#pragma once
#include <cstdint>

class Decoder {
    public:
        Decoder();
        void set_ir(uint16_t ir);
        uint8_t get_opcode(void);
        uint8_t get_op1(void);
        uint8_t get_op2(void);
        bool fetch_literal(void);
        void tick(void);
    private:
        uint16_t IR;
        uint8_t OP1;
        uint8_t OP2;
        uint8_t OPCODE;
};

The operation of this decoder is simple:

  • a call to set_ir will set the desired value for IR.
  • a call to tick will perform the decoding.
  • the get_xxx methods return the appropriate private variables.
  • fetch_literal will return true of the high bit in IR is set.

Decoding

The code stored in IR is decoded as follows (bits are numbered from right to left):

  • bits 10-15 - OPCODE
  • bits 5-9 - OP1
  • bits 0-4 - OP2
  • fetch_literal returns true if bit 15 is a one

We discussed using the >> operator and the logical & operator to do this in class, or you can use the bitset class to break up the bits as needed.

Main Function

Your main code should demonstrate how the decoder works for at least these patterns:

  • IR = 100000 10100 00100
  • IR = 000000 00000 00000
  • IR = 111111 11111 11111
  • IR = 000001 00001 10000
  • IR = 101010 10101 01010

You are to display the final decoding as a set of binary strings similar to those shown above. Use the bitset example shown in the notes for a guide.

Note

Converting these patterns into four hex digits and passing those to set_ir is the easy way to test these.

What to Submit

Create a folder in your cosc2325_projects repository named exam1. Place the code in that folder.

Add a simple Makefile as shown in the lab3 code to build this program. Push the final code to GitHub and you are done.

This is due on Sunday, Feb 25 at Midnight.