Question


• 11- Implementing a circuit using ROM modules. • 1-Build a new module which takes X as an input while the output Y = 2X^2 +
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Holding State

In the previous post, we discussed binary numbers and how the presence or absence of electricity in a circuit can be modeled as a 0 or a 1 respectively.

However, a fundamental problem with electrical circuits is that they are transient. They can’t hold state of their own. For example, from one second to the next, a wire has no idea whether electricity passed through it in the second prior.

Let’s fix that problem with some logic gates. I’ll show you three different images showing different variations of the same circuit.

1*CDfLgv6SKjE4m2HkLwDM8A.png?q=20

1*CDfLgv6SKjE4m2HkLwDM8A.png

Source: CODE by Charles Petzold

The two logic gates being used in the above set of images are NOR gates. Here is the truth table for a NOR gate.

1*VZjjCYRhlqOn3PxoUm5jlw.png?q=20

1*VZjjCYRhlqOn3PxoUm5jlw.png

Source: CODE by Charles Petzold

Let’s look at image one first. The NOR gate on the left has an output of 1 because both its inputs are 0 initially.

If you close the gate on top, the output of the NOR gate on the right becomes 1 because both its inputs are now 0 and the bulb lights up.

Here’s the crazy bit(no pun intended): if you now open the gate on top, the bulb still remains lit!

Why? Because both the inputs to the NOR gate on the right are still 0, so the output remains 1. We just created memory in a circuit!

Now, no matter how many times you open or close the gate on top, the bulb will remain lit. The only way to turn the bulb off is to close the bottom gate while keeping the top gate open.

This circuit is typically called a flip-flop circuit. The circuit illustrated here is more commonly called a Reset-Set flip-flop (R-S flip flop). Here is another way to draw the circuit. The circuit remains the same, just with the light bulb removed and two outputs being shown, one for each NOR gate.

1*KwAVlIuORWQRCmKHWdLgBw.png?q=20

1*KwAVlIuORWQRCmKHWdLgBw.png

Source: CODE by Charles Petzold

Here is the truth table for the same:

1*OIU1Nm1Slr6NFLBMVagyTw.png?q=20

1*OIU1Nm1Slr6NFLBMVagyTw.png

Source: CODE by Charles Petzold

However, the R-S flip flop has an issue associated with it. If you look at the truth table, you’ll notice that if both the inputs are set to 1, the output is indeterminate.

To avoid this, we will use something called a level-triggered D-type latch. Here is the schematic.

1*Geb3XgoaNTy_ptFGCgSS2A.png?q=20

1*Geb3XgoaNTy_ptFGCgSS2A.png

Source: CODE by Charles Petzold

Notice that the right half of the diagram is the exact same as our original R-S flip flop. The left half has two additional AND gates connected to two inputs called the Data input and the Clock input.

The Data input can never provide the same input to both AND gates because it is connected to an inverter. The Clock input typically alternates between 0 and 1, and it serves to determine whether the current data input needs to be remembered or not.

Inputs OutputsD Clock Q Q_bar0 1 0 1 1 1 1 0 X 0 Q Q_bar

There is a difference between having a Clock input of 0 or 1. When the Clock input is 1, the outputs Q and Q_bar reflect the identity and inverse of D respectively. When the Clock input is 0, the input D does not matter and the outputs remain the same.

Let’s attempt to model the level triggered flip flop in code.

This is an IIFE (immediately invoked function execution). I’m using this to maintain an internal state for a function without exposing it to the global scope. You can read more about IIFE’s here.

The flipflop function expects a data and a write parameter. The state is only overwritten if write is 1. Great, we’ve figured out a way to store a bit!

You can run the above code with functions like below:

levelTrigger.flipflop(0, 1)levelTrigger.flipflop(1, 1)levelTrigger.flipflop(0, 0)var result = levelTrigger.getState()

Assembling The Memory

Let’s take the fancy logic gate from the prior figure and represent it as a black box now. We’ll call what we have below a 1-bit latch.

1*Hds2ezIax_xq7idVzIROgQ.png?q=20

1*Hds2ezIax_xq7idVzIROgQ.png

Source: CODE by Charles Petzold

The DO in the above represents the state variable for us.

Now that we can store 1-bit values in a latch, we can store 8-bit values by storing 8 1-bit values in 8 separate latches. The only roadblock is if we have 8 of these latches, how do we differentiate between them?

Simple: we’ll use 3-bit binary numbers. The numbers 0 to 7 represent 8 different values and in binary they go from 000 to 111.

I’m sure you’ve guessed what we’re attempting to do by now. We’re trying to represent each of these functions in memory with the 3-bit binary number representing the memory address. (This is, obviously, a very simplistic model).

Unfortunately, JavaScript doesn’t allow memory access unlike C. For example, in C, you could get access to the address of a function pointer like so:

With JavaScript we’re going to fudge it a little bit. We’ll use a plain JS object to represent our internal memory. Not a bad approximation at all.

I’ve built upon the code we had initially, just replacing the internal state variable with a memoryObj

This is the logic circuit representation of what we’ve built so far.

1*HPqGTjrn1zSW9e3VVhdX-g.png?q=20

1*HPqGTjrn1zSW9e3VVhdX-g.png

Source: CODE by Charles Petzold

It may seem like a massive jump, but look carefully at what the circuit does. There are 8 1-bit latches in the middle and there is a write signal and a data in signal being provided. The address is the 3-bit address representing each of the 8 latches.

The 3-to-8 decoder and 8-to-1 selector are complex logic gates that perform the functionality of targeting specific memory addresses based on the address signal being fed in to them. Something we easily accomplish in a programming language like JavaScript with the key-value pairs of an object.

If you want to understand what the 3-to-8 decoder and 8-to-1 selector do, read here and here. I’m going to skip over it because for our purposes, it is enough to understand that these are circuits through which we feed electric signals to map to a specific memory address.

The Size Of Memory

Let’s think about how much memory we have based on our previous example so far. We have the ability to store 8 1-bit values to give us a total of 8 bits.

What we built above is typically called an 8x1 RAM array. The reason we are limited to 8 bits is because we used a three bit memory address.

Since 2³ = 8, we have the general formula of:

2^n = Total Number Of Bits That Can Be Stored, where n = the number of bits in the memory address.

If we have a four bit memory address where each address can store 1 bit, we would have a 16x1 RAM array like below

1*plXFn-EsYAESSsIhTkFuwA.png?q=20

1*plXFn-EsYAESSsIhTkFuwA.png

Source: CODE by Charles Petzold

Now, if we had a 10 bit memory address, we could store 1024 separate bits because 2¹⁰ = 1024.

But, what if we could store more 8 bits in each memory address? Well, then we’d have a 1024 * 8 = 8192 total bits.

If we extend this, we could have a 16 bit memory address with 8 bits stored in each address.

1*Yd3QI4mRmBAhMIML_jVq6g.png?q=20

1*Yd3QI4mRmBAhMIML_jVq6g.png

Source: CODE by Charles Petzold

The 64K above actually represents 2¹⁶ = 65,536.

Visualizing Memory

Let’s take a moment to make an approximation of what memory “looks” like in a computer. We know we can reference different addresses in memory using an n-bit number, where n can be any number between and inclusive of 1 and 16.

If we were to physically represent this, it might look something like this:

1*KnalGpVkqslduhIno0EkoA.png?q=20

1*KnalGpVkqslduhIno0EkoA.png

Representing memory addresses as blocks

Each of the blocks in the column on the right represent a separate piece of memory. Each piece of memory is composed of 8 1-bit latches which we’ve already seen how to construct.

The column on the left represents the 3-bit address of each of the blocks. We can access the 8-bit number by accessing the memory address for that specific number.

Since we are using a JavaScript object to represent our internal memory, we could access the memory like so: memoryObj[address]

Automation

Okay, now that we’ve figured out how memory is built, let’s try to utilize the memory to perform some basic operations. Like addition, for example.

In the binary adder we built in the previous post, we had the ability to add two 8-bit numbers and get the result. Now, what if we want to add a series of 8-bit numbers and get the final result without having to worry about the intermediate result?

We can store every intermediate result in an 8-bit latch and keep updating the running total!

We already designed an 8-bit adder so we can keep feeding the intermediate result and the next number into the 8-bit adder so that we can keep updating the total.

PLEASE UPVOTE IF YOU FIND THIS HELPFUL.

Add a comment
Know the answer?
Add Answer to:
• 11- Implementing a circuit using ROM modules. • 1-Build a new module which takes X...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT