Forums

Sega Master System / Mark III / Game Gear
SG-1000 / SC-3000 / SF-7000 / OMV
Home - Forums - Games - Scans - Maps - Cheats - Credits
Music - Videos - Development - Hacks - Translations - Homebrew

View topic - Instruction Cycles

Reply to topic
Author Message
Consolemu
  • Guest
Reply with quote
Instruction Cycles
Post Posted: Sat Apr 29, 2000 7:00 pm
In every processor, it takes the CPU a number of cycles to execute an instruction, right? But what exactly are cycles? Is it literally the CPU's circut around the system? And what would be the best way to emulate them? I know their important because there's a lot of cycle time exclusive events that systems rely on such as Hblanks and VBlanks. I was thinking that I need a variable like Cycles and just incrementing it depending on the instructions I was on. Then I would check and see how large Cycles grew and then do an Hblank or a Vblank and when I encountered a Vblank I would reset Cycles. I dunno.

Chris :o)
 
  • Site Admin
  • Joined: 25 Oct 1999
  • Posts: 2029
  • Location: Monterey, California
Reply with quote
Post Posted: Sat Apr 29, 2000 8:27 pm
Quote
> In every processor, it takes the CPU a number of cycles to execute an instruction, right? But what exactly are cycles? Is it literally the CPU's circut around the system? And what would be the best way to emulate them? I know their important because there's a lot of cycle time exclusive events that systems rely on such as Hblanks and VBlanks. I was thinking that I need a variable like Cycles and just incrementing it depending on the instructions I was on. Then I would check and see how large Cycles grew and then do an Hblank or a Vblank and when I encountered a Vblank I would reset Cycles. I dunno.

A cycle is a single 'pulse' of the CPU. The number of cycles that occur in a given second is its clock speed (about 3.8something million per second on the sms's z80). Conversely, you can also say that there are (VERY roughly) 250 nanoseconds, or .25 microseconds between each cycle.
How much gets done in a cycle depends on the architecture of the cpu. The Z80 takes between 4 and (I think) 21 cycles to execute an instruction.

A very simple instruction like:

ld b,a

takes only 4 cycles. It only needs to pull one byte from memory (the single byte opcode itself), update the program counter, and do the the actual instruction itself (which is all taken care of in a single 8 bit operation in internal registers).

add a,b

actually takes the same number of cycles. It's all still contained in a single byte with no other accesses to memory, except for the instruction fetch itself, and is taken care of in a single 8 bit operation. The arithmetic doesn't take any longer than the register to register copy because of the way the chip is engineered.

ld a,(hl)

This takes a bit longer, 7 cycles I think. Again, self contained 1 byte instruction, 8 bit operation, but it has to fetch another byte from memory (whatever hl points to) and put it in a. Evidently, the memory fetch takes about 3 extra cycles.

add hl,de

This takes, I -think- about 11 cycles. The instruction is still a single byte, this time access no other memory other than the instruction fetch, however, the arithmetic must be broken down into two 8-bit steps, since the z80 is an 8-bit processor with an 8-bit ALU. This is still faster than adding with seperate 8-bit math instructions, since you save time on instruction fetch and decoding.

ld a,($c000)

I'm not sure how long this takes, probably about 13-15 cycles (don't have a z80 cycle counting guide handy). The opcode itself is still a single byte, but requires two more bytes to be fetched for the absolute address, plus another byte to fetch from the address itself. It is, at least, an 8-bit operation.

bit 7,a

This is a simple instruciton, which doesn't access memory or have any immediate data (the 7 doesn't count as immediate data.. or does it?). But, the opcode is two bytes long (a necessity, since the z80 has more unique instructions than possibly unique bytes, so some of them must have a single byte 'prefix'). So this operation takes 8 cycles.

ld b,(ix+5)
add a,(iy)

Both of these instructions take a very long time (relatively speaking) to execute, 21 cycles I believe. Each of them require a single byte prefix (as all IX/IY instructions do) and a single byte suffix (the immediate offset, +5 or +0 in the above examples). Plus there's the extra time it takes to add the offset to IX or IY (a 16 bit operation, since there may be a carryover from the low byte to high). And it reads a byte from memory.


On a more physical level, the number of cycles per second is controlled by a crystal timer (acting as a capicitor..?) which by it's own natural properties 'pulses' a certain number of times per second when a certain electrical current is passed through it. The upper limit is the measure of how many cycles can be completed per second without the unit overheating (usually set well below that mark for the sake of reliability). Modern CPU's have much better heat dissapation technologies (plus heat sinks and fans), but these processes cost extra money too (you can buy a z80 for about $5 or so these days, much less in bulk).
  View user's profile Send private message Visit poster's website
Consolemu
  • Guest
Reply with quote
I need a science review...
Post Posted: Sat Apr 29, 2000 9:34 pm
Quote
> A cycle is a single 'pulse' of the CPU. The number of cycles that occur in a given second is its clock speed (about 3.8something million per second on the sms's z80). Conversely, you can also say that there are (VERY roughly) 250 nanoseconds, or .25 microseconds between each cycle.
> How much gets done in a cycle depends on the architecture of the cpu. The Z80 takes between 4 and (I think) 21 cycles to execute an instruction.

So, a microsecond is the 00:00:00 (underscore) on a stopwatch. That number on the far right that generates numbers fast as hell? What's a nanosecond? And 'pulse', what exactly is the pulse. What makes the CPU generate this 'pulse'?

Quote
> A very simple instruction like:

> ld b,a

> takes only 4 cycles. It only needs to pull one byte from memory (the single byte opcode itself), update the program counter, and do the the actual instruction itself (which is all taken care of in a single 8 bit operation in internal registers).

that makes sense cause that's a simple instruction. Probably, like you said, uses one cycle to interpret op code, uses one cycle to increment PC, another cycle to transfer A to B. And uses one final cycle to increment PC again.

Quote
> add a,b

> actually takes the same number of cycles. It's all still contained in a single byte with no other accesses to memory, except for the instruction fetch itself, and is taken care of in a single 8 bit operation. The arithmetic doesn't take any longer than the register to register copy because of the way the chip is engineered.

|*Clipping this part*|
...snip...
|*Restoring this part*|

Quote
> On a more physical level, the number of cycles per second is controlled by a crystal timer (acting as a capicitor..?) which by it's own natural properties 'pulses' a certain number of times per second when a certain electrical current is passed through it. The upper limit is the measure of how many cycles can be completed per second without the unit overheating (usually set well below that mark for the sake of reliability). Modern CPU's have much better heat dissapation technologies (plus heat sinks and fans), but these processes cost extra money too (you can buy a z80 for about $5 or so these days, much less in bulk).

Ah, that's interesting. So when this crystal is zapped with electricity it 'flares' or pulses. Lemme guess. It's pulse is given off in binary form? Like 1 means it has electricty and 0 means it doesn't? And the CPU depends on that 'pulse' to execute instructions? The Megahertz is the amount of pulses that occur in a second, or the amount of instructions? Oh, and what is a hertz? Like you've got hertz, kilohertz and Megahertz. What kind of measurement is that? I've noticed that hertz is used to represent frequency in sound and the speed of light. Like the average human can hear from frequency 20 to 20,000.

Chris :o)
 
  • Site Admin
  • Joined: 25 Oct 1999
  • Posts: 2029
  • Location: Monterey, California
Reply with quote
Re: I need a science review...(you bet)
Post Posted: Sun Apr 30, 2000 1:42 am

Quote
> So, a microsecond is the 00:00:00 (underscore) on a stopwatch. That number on the far right that generates numbers fast as hell? What's a nanosecond? And 'pulse', what exactly is the pulse. What makes the CPU generate this 'pulse'?

You think that's fast?
Your sub-second measurement is probably 1/60th of a second, or possibly 1/100th second.
A microsecond is 1/1000000th of a second. One millionth. Much, much faster than you can perceive.
Faster yet is a nanosecond. One billionth (1/1000000000th) of a second. A 1-gigahertz processor cycles 1 billion time a second. That's very fast.

A review of prefixes:
'tera' - 1,000,000,000,000 (trillion)
'giga - 1,000,000,000 (billion)
'mega' - 1,000,000 (million)
'kilo' - 1,000 (thousand)
- -
'milli' - 1/1,000 (thousandth)
'micro' - 1/1,000,000 (millionth)
'nano' - 1/1,000,000,000 (billionth)
(i forget which one means trillionth... 'peta'?)

So a nanesecond is 1 second times 1/1,000,000,000 or 1 billions of a second. millisecond is 1/1,000th of a second. A megabyte should be 1,000,000 bytes but since bytes (usually!) measured in numbers that are 'round' in hex or binary, it's actually 1048576 (1024 x 1024). Although sneaky hard drive manufacturers use 'metric' megabytes (actually 1,000,000).

Quote
> > A very simple instruction like:


> that makes sense cause that's a simple instruction. Probably, like you said, uses one cycle to interpret op code, uses one cycle to increment PC, another cycle to transfer A to B. And uses one final cycle to increment PC again.

why would you increment the PC twice?
It's probably not that simple, but it's not one of those things that's necessary to know (even for an emulator programmer).
Probably more like 3 cycles to fetch the instruction, and one to do all the rest.


Quote
> ...snip...
> |*Restoring this part*|
>

> Ah, that's interesting. So when this crystal is zapped with electricity it 'flares' or pulses. Lemme guess. It's pulse is given off in binary form? Like 1 means it has electricty and 0 means it doesn't? And the CPU depends on that 'pulse' to execute instructions? The Megahertz is the amount of pulses that occur in a second, or the amount of instructions? Oh, and what is a hertz? Like you've got hertz, kilohertz and Megahertz. What kind of measurement is that? I've noticed that hertz is used to represent frequency in sound and the speed of light. Like the average human can hear from frequency 20 to 20,000.

I wouldn't say the crystal is 'zapped', rather there is a steady flow of electricity flowing through it. It stores it up to a certain point, and then discharges it when it reaches a certain capacity. So long as the supply of power to the crystal is constant, it will discharge at a constant rate (around 3.8 million times a second).. and the amount of energy is discharges will also be constant.
The crystal is seperate from the cpu (usually). I think the energy that discharges into the CPU actually powers the CPU, but I am not certain of this.
The clock rate of the processor is the amount of cycles (as the number of instructions in a second is not constant... especially not on a z80).
(the flow of electricity between the crystal and the CPU does not depend on any binary definitions, you could say the signal is '1' when electricity is discharging and '0' when it isn't, or you could define it the opposite way, but there's no reason to define it either way).


A 'hertz' is a generic term of measurement, meaning 'one cycle per second'. The term does not specify what sort of cycle, only that whatever it is happens that many times in a second.

Megahertz means 1 million cycles a second. A gigahertz is 1 billion cycles persecond. You normally wouldn't use a greater-than-one prefix on 'hertz', but I suppose you could say millihertz is one cycle every 1,000 seconds.

In the science of accoustics, a 'pure tone' is a a sinusoidal sound wave that completes it's crest-to-crest cycles at a given frequency. A 440 hertz tone goes from crest to crest 440 times a second (a 440 hertz tone is a middle 'A' note, btw).
More complicated sounds are built out of sinusoidal 'pure' tones of various frequencies, amplitudes, and phases, changing over time.
But I'm not going to get into that.
  View user's profile Send private message Visit poster's website
Jose Manuel
  • Guest
Reply with quote
Post Posted: Sun Apr 30, 2000 11:40 am

Hello Chris :)

The Z80 processor, like all processorss, is a "combinational" digital circuit, so if you read a digital electronic book, you see that need a electrical signal called "clock". What this clock is? There are a square electrical signal _|"|_|"|_|"|_|"|_|"|_|"|_|"|_|"|_ like this. Thus, the voltage goes from 0 to 5 volts, wait a certain time and falls to 0 volts, wait certain time. If the signal is simetric (in most processorrs is) the the high period and the low period are the same (sorry, the period is the time the signal is in one state). So if you know the frecuency of the signal, you can calculate the period via the formula: period = 1 / frecuency. Like the signal is simmetric, here the period is the time the signal repeate itself, the time the signal makes this: _|"|, and repeast again and again (play it again, Sam :) ).

The question now is.... What the hell the processor needs this signal? The procesor is a finite state machine, so it have a number of states, like: now, we have data onto acumulator, now im writing data onto the bus, now i have read one instruction and parsing onto the decoder, etc.... So in each tick of the clock, the processor changes the state. Notice that can be pipelinig (yes, the Z80 have little pipelinig): if you have to do things in different parts of the processor, you can do at the same time, if they not interfered one which other. So a complete instruction, like ADD, spends a fixed number of states of the processor: read the instruction, decodificate them, read the operands, Add it on the ALU, and place the result in the proper register or memory location.

I wish that this explanation will help you, not "ofuscate" you more :)

Saludos,

JMD
 
  • Joined: 18 Sep 1999
  • Posts: 498
  • Location: Portland, Oregon USA
Reply with quote
Post Posted: Sun Apr 30, 2000 6:26 pm

Quote
>
> Hello Chris :)

> The Z80 processor, like all processorss, is a "combinational" digital circuit, so if you read a digital electronic book, you see that need a electrical signal called "clock".

No. "Combinational" circuits do not require a clock. Combinational circuits have no feed-back loops and don't carry state. They don't need clocks. A simple AND-OR network is an example of a combinational circuit. A combinational circuit's output only depends on its current inputs.

The Z80 processor, and others, are "Sequential" circuits. They contain state, and to avoid "race conditions" typically use a clock to synchronize state transitions. A sequential circuit's next state output depends on its current state and current inputs.

Eric Quinn
  View user's profile Send private message Visit poster's website
  • Joined: 18 Sep 1999
  • Posts: 498
  • Location: Portland, Oregon USA
Reply with quote
Re: I need a science review...(you bet)
Post Posted: Sun Apr 30, 2000 6:37 pm
Quote
> A review of prefixes:
> 'tera' - 1,000,000,000,000 (trillion)
> 'giga - 1,000,000,000 (billion)
> 'mega' - 1,000,000 (million)
> 'kilo' - 1,000 (thousand)
> - -
> 'milli' - 1/1,000 (thousandth)
> 'micro' - 1/1,000,000 (millionth)
> 'nano' - 1/1,000,000,000 (billionth)
> (i forget which one means trillionth... 'peta'?)

Here you go:


Factor Prefix Symbol
10^18 (quintillion) exa E
10^15 (quadrillion) peta P
10^12 (trillion) tera T
10^9 (billion) giga G
10^6 (million) mega M
10^3 (thousand) kilo k
10^2 (hundred) hecto h
10^1 (ten) deka da
10^0
10^-1 (one-tenth) deci d
10^-2 (one-hundredth) centi c
10^-3 (one-thousandth) milli m
10^-6 (one-millionth) micro u
10^-9 (one-billionth) nano n
10^-12 (one-trillionth) pico p
10^-15 (one-quadrillionth) femto f
10^-18 (one-quintillionth) atto a

Eric Quinn

  View user's profile Send private message Visit poster's website
Jose Manuel Delgado
  • Guest
Reply with quote
Post Posted: Mon May 01, 2000 11:30 am
Hello :)

Yes, you are rigth Eric. Thanks by correct the mistake. The processor is a secuencial system, and uses the clock to avoid spurios transitions between states. Thanks again.

Saludos,

JMD
 
Reply to topic



Back to the top of this page

Back to SMS Power!