teaching machines

CS 352 Lecture 14 – Program Counter

October 7, 2016 by . Filed under cs352, fall 2016, lectures.

Dear students,

The next homework is due in a few days, and I want to revisit some HDL concepts in hopes to roust up any questions you have. In particular, the advanced chips can benefit from array slicing, which we haven’t talked about yet. We’ll start today with an example of a hardware component that does an endian swap.

Endianness refers to how we lay out multibyte data in byte-addressable memory. Is the value at the address the least significant bits of the value, or the most significant, or something else altogether? Big endian architectures put the most significant bits (the big end) at the address. Little endian architectures put the least significant bits at the addressed byte. As far as I’m aware, there’s no great advantage of one of the other. But there’s a great disadvantage in that both choices must be anticipated. When you share multibyte data with someone, you must be aware with which endianness they encoded it and swap the order as necessary. Many protocols and file formats prescribe the endianness of their multibyte data. To write a conformant file, you must swap the order if your machine does not match the prescribed order.

The names are apt and come from Gulliver’s Travels, a satirical novel published in 1726. Through it, British author Jonathan Swift poked fun at his country. Here’s the excerpt relevant to our discussion:

Besides, our Histories of six thousand Moons make no mention of any other Regions, than the two great Empires of Lilliput and Blefuscu. Which two mighty Powers have, as I was going to tell you, been engaged in a most obstinate War for six and thirty Moons past.

It began upon the following Occasion. It is allowed on all Hands, that the primitive way of breaking Eggs, before we eat them, was upon the larger End: But his present Majesty’s Grand-father, while he was a Boy, going to eat an Egg, and breaking it according to the ancient Practice, happened to cut one of his Fingers. Whereupon the Emperor his Father published an Edict, commanding all his Subjects, upon great Penaltys, to break the smaller End of their Eggs.

The People so highly resented this Law, that our Histories tell us there have been six Rebellions raised on that account; wherein one Emperor lost his Life, and another his Crown. These civil Commotions were constantly fomented by the Monarchs of Blefuscu; and when they were quelled, the Exiles always fled for Refuge to that Empire. It is computed, that eleven thousand Persons have, at several times, suffered Death, rather than submit to break their Eggs at the smaller End.

Many hundred large Volumes have been published upon this Controversy: But the books of the Big-Endians have been long forbidden, and the whole Party rendered incapable by Law of holding Employments. During the Course of these Troubles, the Emperors of Blefuscu did frequently expostulate by their Ambassadors, accusing us of making a Schism in Religion, by offending against a fundamental Doctrine of our great Prophet Lustrog, in the fifty-fourth Chapter of the Brundrecal (which is their Alcoran.) This, however, is thought to be a meer Strain upon the Text: For the Words are these: That all true Believers shall break their Eggs at the convenient End: and which is the convenient End, seems, in my humble Opinion, to be left to every Man’s Conscience, or at least in the power of the Chief Magistrate to determine.

Now the Big-Endian Exiles have found so much Credit in the Emperor of Blefuscu’s Court, and so much private Assistance and Encouragement from their Party here at home, that a bloody War has been carried on between the two Empires for six and thirty Moons with various Success; during which time we have lost forty Capital Ships, and a much greater number of smaller Vessels, together with thirty thousand of our best Seamen and Soldiers; and the Damage received by the Enemy is reckon’d to be somewhat greater than Ours. However, they have now equipped a numerous Fleet, and are just preparing to make a Descent upon us; and his Imperial Majesty, placing great Confidence in your Valour and Strength, has commanded me to lay this Account of his affairs before you.

Swift’s impact on technology could not have been predicted. First endianness and later Apple’s new language!

We’ve built the following hardware components central to a computer’s operation:

Let’s add our final core chip: the program counter (PC). This component serves as the i that drives the flow of control through our program. Our code will be treated like an array, and the PC will march us through it. We’ll build up the component “bit by bit” using the following steps:

  1. Design a circuit that shows a settable count/address on a counter display.
  2. Tweak the circuit so the counter shows a count that’s been remembered. The count is remembered only when the load bit is set.
  3. Tweak the circuit so the counter can either be loaded with a particular address or with whatever the previous address was plus 1.
  4. Tweak the circuit so the counter is automatically incremented by an oscillator.

See you next class!

Sincerely,

P.S. It’s Haiku Friday!

Who moves the clock’s hands?
It does. Thank goodness it does
I don’t have the time

P.P.S. Here’s the code we wrote together:

Buffer.hdl

CHIP Buffer {
  IN in;
  OUT out;
  PARTS:
  Not(in=in, out=tmp);
  Not(in=tmp, out=out);
}

Buffer8.hdl

CHIP Buffer8 {
  IN in[8];
  OUT out[8];
  PARTS:
  Buffer(in=in[0], out=out[0]);
  Buffer(in=in[1], out=out[1]);
  Buffer(in=in[2], out=out[2]);
  Buffer(in=in[3], out=out[3]);
  Buffer(in=in[4], out=out[4]);
  Buffer(in=in[5], out=out[5]);
  Buffer(in=in[6], out=out[6]);
  Buffer(in=in[7], out=out[7]);
}

Swapper.hdl

CHIP Swapper {
  IN in[16]; 
  OUT out[16]; 
  PARTS:
  Buffer8(in=in[0..7], out=out[8..15]);
  Buffer8(in=in[8..15], out=out[0..7]);
}