Applications 4
Exercise 1
Caesar cipher
The Caesar cipher is the simplest substitution cipher. A plain text message is encrypted by replacing each letter from the message by another one that is some fixed number of positions down the alphabet. All letters of the alphabet are changed using the same fixed number of positions. The letters are “shifted” down the alphabet, this code being also named shift cipher. In this exercise and the following ones, we assume that the plain and encoded messages contain only ASCII uppercase letters A to Z, without blank spaces between words. The encryption is done such that A-Z set of letters are translated to letters from the same set A-Z. For example, if the shift is of 3 positions down the alphabet, A translates to D, B to E, C to F, a.s.o. To achieve the circularity of the encoding, W translates to Z, X to A, Y to B and Z to C.
ASCII code
The ASCII code is the widest used character encoding standard in digital technology. Originally limited to 7 bits it usually uses 8 bits to code for the Latin alphabet letters, uppercase and lowercase, digits, punctuation marks, some special symbols, and some commands. We need to know the ASCII codes for the uppercase letters of the alphabet:
Letter | A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z |
hex code | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 4a | 4b | 4c | 4d | 4e | 4f | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 5a |
Caesar encoder
Your goal is to design a configurable Caesar encoder, where the shift is set from the outside. The circuit receives an ASCII letter on data_in input, shifts it by a number of positions given by the key input and outputs the resulting letter as data_out. It is supposed that the input data are restricted to the A-Z ASCII codes and the shift is an unsigned integer no greater than 26 (the length of the alphabet)
The implementation adds the shift number to the ASCII code of the input letter and corrects the sum such that the output ASCII code is also in the A-Z range of ASCII codes.
assign data_out = data_in + shift > 8'd90 ?
data_in + shift - 8'd26 :
data_in + shift;
Caesar decoding
The Caesar encrypted message is easy to decode: we need only to shift back all letters to undo the coding. Of course the decoder needs to know the shift number (by the way, the Caesar code may be easily broken by trying for the encrypted message various shifts until a meaningful message is found). To reverse the coding of our Caesar encoder, the decoder uses shift_decode = 26 - shift_code
Testbench
The testbench instantiates two Caesar encoder modules. The first one is the encoder itself, that generates the encoded message. The second instance uses the reversed shift to decode the encrypted message. The shift value (for example 13) may be assigned directly to the shift port of the instance as a literal constant:
caesar encoder (
.shift (8'd13),
// other port connections
);
The constant value assigned to an input port is actually a hardwired connection of its bits to the power supply and to the ground.
The testbench generates a sequence of ASCII values for the data_sent input of the encoder. The verilog language allows you to assign to an 8-bit logic variable an ASCII value as a character:
initial begin
#0 data_sent = "C";
#1 data_sent = "O";
#1 data_sent = "M";
#1 data_sent = "B";
#1 data_sent = "I";
#1 data_sent = "N";
#1 data_sent = "A";
#1 data_sent = "T";
#1 data_sent = "I";
#1 data_sent = "O";
#1 data_sent = "N";
#1 data_sent = "A";
#1 data_sent = "L";
#1 $stop;
end
To display the ASCII values on the waveforms in simulation, right-click the name of the variable in the Simulation panel and select from the pop-up menu radix -> ASCII
Exercise 2
Make a 7-segment display transcoder using a case block that displays values from 0 to 9. The input of the module will be called value and the output seg.
The output bits would control the segments of a digit display, one bit for each segment. The segment is lit when the corresponding bit is 0, otherwise it is off (negative logic).
For all numbers greater than 9 the digit must be off. All these 6 cases may be covered in a single assignment under the default case.
Because seg values are easier to handle in binary, the assigned values for seg should be given as binary constants. A number in binary format (or a combination of bit values) must be preceded by the base specificator, 'b
, according to the template numberOfBits'bvalue as in the following examples:
8'b11 |
3, in binary format on 8 bits |
8'b00000011 |
3, in binary format on 8 bits. Leading zeros are explicitly written |
8'b0000_0011 |
when there are too many bits, the underline, _ may be used to split bits into smaller groups |
display.
Coding style:
- case statements should always include the default case.
The transcoder may be extended to show hexadecimal digits greater than 9. Use the most common 7-segment symbols for them: