One-week intensive course on
Hardware Description Languages for Synchronous Circuits

Gordon Pace, Koen Claessen

Exercises: Hardware Compilation

Thursday 7th September

Note: Exercise 1 is for everyone. In exercise 2, you can choose to do 2-I (for Haskell programmers), or 2-II (for non-Haskell programmers).

1. MiniFlash Programs
In today's lecture, we saw the simple programming language MiniFlash. To get a feeling for MiniFlash, you are going to write some simple programs in MiniFlash. If you want to, you can write them directly in Lava.

Exercises
a. Write the always circuit from Monday, exercise 3, as a MiniFlash program.

b. Write a delay circuit in MiniFlash, where the output of the compiled program is the same as the input but delayed by one time unit.

c. Yet another bomb detonator, but this time, the user has to press the buttons a, b and c in any order (even at the same time) before the bomb explodes. (This is the same as Tuesday's exercise 1.)

d. Consider the following programming construct, so called big case:

        if cond1 then
          prog1
        elseif cond2 then
          prog2
        elseif
          ...
        elseif condn then
          progn
        endif
      

Is there an optimization that we can apply if we are guaranteed that the several conditions are all exclusive (meaning only one of them can be true at the same time)?

e. Are the following two MiniFlash programs equivalent:

        forever (P1 || P2)
      
and
        forever P1 || forever P2
      
?

2-I. Updatable variables
(For Haskell programmers)
An implementation of a simple version of MiniFlash is available. In this exercise, we will extend MiniFlash with an updatable variable.

Exercises
a. Add an updatable variable to MiniFlash. This means that you will add one extra construct to MiniFlash, update, which gives the variable a new boolean value. This construct takes one clock tick to execute. Extend MiniFlash's datatype in the following way:
        data MiniFlash = ...
                       | Update (Signal Bool)
      

You should also provide a way for the programmer to read the value of the variable. The easiest thing is to have an extra output.

b. What happens when two programs in parallel want to update the variable at the same time? Add an extra error output to the generated circuit that signals such an event.

c. Write a small example program using your variable, and test or verify that the program never generates the error.

d. (*) Add a mechanism so that it becomes possible to have several updatable variables in your program.

e. (***) How would you implement a variable assignment that does not take any clock time to execute?

2-II. Exceptions
(For non-Haskell programmers)
In this exercise, we will extend MiniFlash with exceptions. This requires two new operations: throw and catch.

program ::= ...
| Throw
| Catch program

The intended semantics is as follows: a program can be surrounded by a catch. If such a program executes a throw, the program immediately stops and the surrounding catch statement terminates. All other processes within the catch are terminated too.

For example, the following MiniFlash program:

      Catch
      ( forever
        ( If "kill" Then
            Throw
          Else
            Emit "going"
        ; Delay
        )
     || forever
        ( Emit "me too"
        ; Delay
        )
      )
    ; Emit "done"
    
behaves as follows: it continuously sends the signals "going" and "me too". But if the input "kill" is high, then "done" is output for one time unit, and then the program falls silent.

Exercise
Design how MiniFlash exceptions should be compiled into hardware. You can do this by drawing pictures like the ones we saw in today's lecture.

Hint: The problem is that, in a program Catch (A || B), when A throws an exception, B has to be stopped too. So, every subprogram can possibly be killed by an exception in a parallel thread. Make this possible by distributing a kill wire to every subprogram, which stops execution of that program. This requires a change in the compilation of some of the other MiniFlash constructs as well.