1

I am a huge learn-by-example person, which means describing it is typical leaves me at a loss, for my learning style, because I need to see first, and then any explanation can be realized. I have been playing with the Nand2Tetris program's Hack Assembly Language .asm files, and creating snippets to study, use and learn from.

My question is if I wanted to clock time, for example, how can I time or tick-tock the time it takes to run a function? I'm looking at the DFF gate as a possible solution, but I'm very confused about how to even go about using it for this task and need to see a solid example code. I am hoping someone can give me a code example of how to code this in ASM format.

For example:

  • Store TimeStart How to store a time or tick to variable?

  • Count 1-100000 I already have this part done.

  • Store TimeEnd How to store a time or tick to variable?

How to translate TimeEnd - TimeStart to something legible?

Is this possible?

DuDa
  • 3,718
  • 4
  • 16
  • 36
A Nichole
  • 13
  • 3
  • I didn't realize it was possible to do this in asm; I would have thought to the test/script system for that. – Erik Eidt Nov 04 '20 at 23:25

2 Answers2

1

There is no way to read a clock / tick time programmatically; the Hack machine has no way to get that info.

Similarly, the CPU Emulator doesn't have the ability to do this, though having the option to dump an instruction trace would be nice (perhaps something to suggest for the future).

You can of course manually trace your code and figure out how many instructions are executed. Since each instruction takes a single cycle, this is relatively straightforward.

If your are absolutely desperate, I wrote a python version of the CPU Emulator that emulates the machine at the functional-unit level as part of a project to implement the Hack CPU in relay logic. It can trace out instructions and could potentially be modified to do this for you. You can find it here: https://github.com/RJWoodhead/Relay2Tetris/blob/master/Simulator.md

MadOverlord
  • 1,034
  • 6
  • 11
0

As @MadOverlord correctly points out:

... the Hack machine has no way to get that info.

However, I notice that you're trying to come up with a solution using low level gates like DFF. Presumably you're looking at DFF because of reading things like "... the DFF has a clock input that continuously changes according to the master clock's signal", Chapter 3, page 42. I think perhaps you misunderstand the nature of the clock referenced here and clearing up that misunderstanding will be useful.

At a high level, this clock represents a discrete unit of time which governs when the CPU executes operations, but it does not keep track of the passage of time in human units. Here are the relevant excerpts from Chapter 3, page 42:

  • "The elapsed time between the beginning of a "tick" and the end of the subsequent "tock" is called cycle, and each clock cycle is taken to model one discrete time unit."

  • "The current clock phase (tick or tock) is represented by a binary signal"

  • "Using the hardware’s circuitry, this signal is simultaneously broadcast to every sequential chip throughout the computer platform."

The need for this clock is to allow the computer's internal hardware to have enough time for the machine's memory to get into the appropriate state before executing the next operation. For example, if you add two numbers together, you need to wait for all of the bits in memory to flip to the appropriate positions before you can move on and use the result of that addition in another operation. Hardware operations, although fast, require different lengths of time to complete for various tasks, and so the computer must wait a small amount of time between operations. This small amount of time is the discrete unit of time that elapses between clock cycles.

On the other hand, keeping track of the passing of time in human units is something typically provided by another piece of hardware called a real-time clock. This is separate from the CPU because its behavior is not needed by the CPU and would therefore only make the CPU implementation less efficient. As far as I know, nand2tetris does not model a real-time clock in any meaningful way for the user (correct me if I have this wrong).

I'm skipping over a lot of the details, but I feel like this line of thought, at this level, is maybe enough to help you think through why your idea won't work. Or maybe your idea will work and I'm missing something, let me know!

Best of luck.

z2e3r40o
  • 56
  • 6