Project 2: Apocalypse ISA Interpreter
Due: Monday, March 12, 2018, at 11:59pm

Description

An Interpreter is a program that takes in a file of code, (in this case the human-readable mnemonic representations of machine instructions) and simulates their execution.

In this project, you will write an assembler for the Apocalypse CPU from 447 whose instructions are shown below. The input file format is also shown below.

Apocalypse ISA

Instruction

Action

add $RD, $RS, $RT

$RD = $RS + $RT

sub $RD, $RS, $RT

$RD = $RS - $RT

slt $RD, $RS, $RT

$RD = $RS < $RT

addi $RT, $RS, Immediate

$RT = $RS + Immediate

beq $RS, $RT, Immediate

If $RS==$RT then $PC=$PC+2+Immediate<<1

xor $RD, $RS, $RT

$RD = $RS ^ $RT

sb $RT, Immediate($RS)

Memory[$RS + Immediate] = $RT

j Immediate

$PC = Immediate

lb $RT, Immediate($RS)

$RT = Memory[$RS + Immediate]

rand $RT, $RS

$RT = random number [0, $RS-1]

disp $RS

Display $RS on the 2 digit hex display

halt

Stops the CPU

Input Format

The input file format will only have a default .text section which contains the assembly code.

The opcodes can be labeled. The labels are valid C identifers (must start with letter or underscore, after that can also include numbers). They are suffixed by a colon.

Opcodes and registers are to be specified in lowercase. Registers are $r0-$r3.

Immediates can be in decimal or hexadecimal (with leading 0x).

Comments are line comments beginning with #.

label2: OPCODE $R0, $R1, 0x20

     J label2

# this is a comment

Approach

First, you will need to create a tokenizer in JFlex that returns symbols for the parser to consume. You may use the Symbol class from JavaCUP, read the JFlex manual about %cup and the CUP examples. Discard the comments and whitespace.

You have some decisions about how to structure the token types that you return from the lexer. You could return each opcode separately or group them. I’d suggest having at least a way to distinguish between R-type, I-type, and jump opcodes so that the parser has an easier job of knowing when you’ve put an immediate where you shouldn’t have, etc.

You have similar decisions about dealing with registers and integers. Try to plan this step with your grammar in mind.

For your grammar, make sure you’re only accepting legal operands. The parser should produce a list of Instructions. How you create the object hierarchy for that is up to you, but at least there should be an interface or base class Instruction. There is no tree necessary, so our intermediate representation is more of an “abstract syntax list”.

To do interpretation, iterate over your list, and do the work associated with each instruction.

Operation

There will be four 8-bit registers: $r0, $r1, $r2, and $r3. All will hold readable and writeable data. To set a register to zero, simply xor it with itself.

As you find labels in the parsing phase, use a hashtable to store the instruction number that had an associated label definition. During execution, turn that label into an instruction index for your $PC.

We support a disp instruction that displays the contents of the specified register as a 2-digit hexadecimal number on a line.

RAM is simulated as a bank of 8-bit values. Addresses are also 8-bits, meaning we have 256 memory locations to work with. This could be implemented as an array or a hashtable.

Requirements

·         Use JFlex to implement an Apocalypse Assembly file lexer that returns symbols to the parser

·         Use JavaCUP to implement a parser and construct a list of Instructions

·         Support labels via a hashtable

·         Consume files written in the input format above

·         Support the instructions listed in the table above

Submission

By the deadline, you need to submit:

1.       Your JFlex file containing your lexer

2.       Your JavaCUP file containing your parser

3.       Your java files containing main() and any auxiliary Java files you have used

4.       A Makefile to build it all

5.       A README text file describing how to run it

6.       Any examples that you have tested your program on

Create a zip file of the above files and upload to unixs.cis.pitt.edu or thot.cs.pitt.edu and then copy your file to:

~jrmst106/submit/1622