Counting T states in Excel

The place for codemasters or beginners to talk about programming any language for the Spectrum.
Post Reply
User avatar
Turtle_Quality
Manic Miner
Posts: 502
Joined: Fri Dec 07, 2018 10:19 pm

Counting T states in Excel

Post by Turtle_Quality »

Ok so I'm very slowly writing this block dropping game, it's my new year's resolution but maybe should have been new decade resolution.

Anyhoo, as it has character movement and RST16 or other Rom calls are quite slow, I started looking around and found some code I could use. Found a neat routine by Dean Belfield. Then I thought I should be able to optimise it. But I don't see the cycles per instruction in my assembler, and even if it could, the assembler can't know how often given instructions will be used.

Which lead me off on yet another tangent, an Excel I knocked together which you can find here (with the string print code snippets as an example)

https://drive.google.com/open?id=1eERwD ... ONW0unqsVR

Put in the instruction codes, or copy in info from a text file (tabs separating labels from opcodes from inline comments), and it shows me the T states. Then I can mark how often an instruction is run (would be the same for a straight sequence) plus for conditionals, how often it's true or false. Then with excel, totting up subtotals and totals is a piece of cake.

Note, that to match the opcodes I had to keep them generic, by moving operands - call labels and other absolutes to the next column. But it's easy to see which ones need fixing and that's not a great deal of editing.

It's very rough and ready, something I set up in my lunchbreak. Maybe I'll return to this and make it a full VBA application (VBA development was a large part of my job between 2006 - 2011), or maybe someone will point out there's something better out there. But if it's saved someone else a few t-states then er... that's collateral optimisation

And if I get anywhere optimising the code I'll bring that up in a separate thread. Eventually you might even see a game from me
Definition of loop : see loop
User avatar
Turtle_Quality
Manic Miner
Posts: 502
Joined: Fri Dec 07, 2018 10:19 pm

Re: Counting T states in Excel

Post by Turtle_Quality »

Well my first attempt at sharing an Excel on a forum took an unexpected twist, Google docs tries to display the Excel in their app.

Let's try again with a link to the zip

https://drive.google.com/open?id=1rdx4g ... qIPnsCDTWx
Definition of loop : see loop
User avatar
Ast A. Moore
Rick Dangerous
Posts: 2640
Joined: Mon Nov 13, 2017 3:16 pm

Re: Counting T states in Excel

Post by Ast A. Moore »

With practice, you’ll learn the timings of most instructions by heart. Besides, it’s usually easy to calculate them if you know how instructions are handled by the CPU.

An interesting project nevertheless.
Every man should plant a tree, build a house, and write a ZX Spectrum game.

Author of A Yankee in Iraq, a 50 fps shoot-’em-up—the first game to utilize the floating bus on the +2A/+3,
and zasm Z80 Assembler syntax highlighter.
User avatar
djnzx48
Manic Miner
Posts: 729
Joined: Wed Dec 06, 2017 2:13 am
Location: New Zealand

Re: Counting T states in Excel

Post by djnzx48 »

Now that is rather interesting. I've seen sprites and game maps designed using Excel, and now it seems developing a whole Spectrum game is possible. It's quite useful to have the code in resizable columns as well.

By the way, you can save 21 T-states in your Print_Char routine by changing it to this:
Spoiler
LD L,A
XOR A
LD H,A
ADD HL,HL
ADD HL,HL
ADD HL,HL
LD BC,&3C00
ADD HL, BC
What I'd like to see is a debugger that listed the timings next to each instruction, taking contention into account. Maybe the vertical size of the instructions could change relative to their T-state count, so you could visually see which ones are taking the longest, and where all the contended time is going.
User avatar
arkannoyed
Manic Miner
Posts: 435
Joined: Mon Feb 05, 2018 9:56 am
Location: Northamptonshire

Re: Counting T states in Excel

Post by arkannoyed »

Provided you are only printing the usual range of characters from 00 to 7f then you can do it even shorter and faster;

Code: Select all

Add a,a
Ld l,a
Ld h,0f
Add hl,hl
Add hl,hl
37 t states only.
6 bytes
User avatar
Ast A. Moore
Rick Dangerous
Posts: 2640
Joined: Mon Nov 13, 2017 3:16 pm

Re: Counting T states in Excel

Post by Ast A. Moore »

djnzx48 wrote: Tue May 21, 2019 12:53 am What I'd like to see is a debugger that listed the timings next to each instruction, taking contention into account.
That would be quite difficult to implement, since the debugger would have to essentially pre-run your code. However, some assemblers—zasm, for example—have an option of generating the listing with T states indicated alongside the instructions:

Image
Every man should plant a tree, build a house, and write a ZX Spectrum game.

Author of A Yankee in Iraq, a 50 fps shoot-’em-up—the first game to utilize the floating bus on the +2A/+3,
and zasm Z80 Assembler syntax highlighter.
User avatar
djnzx48
Manic Miner
Posts: 729
Joined: Wed Dec 06, 2017 2:13 am
Location: New Zealand

Re: Counting T states in Excel

Post by djnzx48 »

I'm pretty sure it would be possible. Since the machine behaviour is deterministic, you could just have another emulation running ahead of the main one that remembers the instruction timings. If it encountered a backwards jump to the current PC, it would just stop there until you get back to that point while single-stepping.

And if backwards jumps became a problem, maybe the instructions could be ordered in the debugger based on time, rather than memory address. So CALLs essentially become inlined and loops become unrolled. It would still be kind of useful IMO.
User avatar
Turtle_Quality
Manic Miner
Posts: 502
Joined: Fri Dec 07, 2018 10:19 pm

Re: Counting T states in Excel

Post by Turtle_Quality »

Well this Excel isn't assembling so you can't write a game with it. I just thought it was a way to check snippets of looped code and how to speed them up.
I often think I see a faster way only to find out it isn't. My sprite code I wrote when I was 17 used IX+d to obtain screen addresses from a lookup table because I was assuming fewest operations = fastest code
Definition of loop : see loop
Ralf
Rick Dangerous
Posts: 2279
Joined: Mon Nov 13, 2017 11:59 am
Location: Poland

Re: Counting T states in Excel

Post by Ralf »

An interesting approach but wise men generally don't do it this way ;)

First when you get more experience you'll remember most of these execution times by heart.

Then in most cases you just don't count these tstates. You write something that seems to be good enough,
run it and if it actually runs good enough then you leave it.

I know a story of a guy who was writing a game for Amstrad CPC and he really, really wanted to optimize it. So he started writing
some tools. Eventually he never made the game as working on the tools totally sucked him in. So you should know not to overdose
on optimisation ;)

Of course you should do whatever suits you, I just wanted to share some of my experience.
User avatar
djnzx48
Manic Miner
Posts: 729
Joined: Wed Dec 06, 2017 2:13 am
Location: New Zealand

Re: Counting T states in Excel

Post by djnzx48 »

It depends what you're trying to optimise I suppose. If it's a sound loop that has to output at a very precise rate, or a graphics loop that has to complete before the raster overtakes it, then it is worth working out the exact execution times so you'll know whether it's fast enough or if something has to be dropped. But if you're only drawing a handful of character blocks on the screen, it may be better to just write anything as long as it works, and optimise it later.

The worst thing is when you don't want to start writing a routine yet, because you think it won't be small/fast enough on the first attempt. So you delay it for ages and do nothing. Or you spend too long focusing on a single routine and never get anything done for the rest of the game. It's always better to just write something at least, and even if it does turn out to be too slow, you'll have a starting point that can be improved upon.
User avatar
arkannoyed
Manic Miner
Posts: 435
Joined: Mon Feb 05, 2018 9:56 am
Location: Northamptonshire

Re: Counting T states in Excel

Post by arkannoyed »

I seem to recall an ancient thread on WOS many years ago, where a useful utility that went by the name of 'Ticks' or something similar could be used to give accurate T-state timings for a piece of assembly code. Googling it though, I can't find it now.
User avatar
djnzx48
Manic Miner
Posts: 729
Joined: Wed Dec 06, 2017 2:13 am
Location: New Zealand

Re: Counting T states in Excel

Post by djnzx48 »

Is this it? ticks.0.15.zip
User avatar
Ast A. Moore
Rick Dangerous
Posts: 2640
Joined: Mon Nov 13, 2017 3:16 pm

Re: Counting T states in Excel

Post by Ast A. Moore »

djnzx48 wrote: Tue May 21, 2019 9:21 am I'm pretty sure it would be possible. Since the machine behaviour is deterministic, you could just have another emulation running ahead of the main one that remembers the instruction timings.
Generally, true. The problem is mostly with branching and conditionals.

Most debuggers display the current number of T states elapsed since the beginning of the frame, anyway. Most of the time, that’s enough. If I optimize my routines, I simply put the number of T states for each instruction in square brackets in the code’s commentary and calculate them manually. However, I still mostly use my gut feeling and experience for optimization. Seems to work best.
Every man should plant a tree, build a house, and write a ZX Spectrum game.

Author of A Yankee in Iraq, a 50 fps shoot-’em-up—the first game to utilize the floating bus on the +2A/+3,
and zasm Z80 Assembler syntax highlighter.
User avatar
arkannoyed
Manic Miner
Posts: 435
Joined: Mon Feb 05, 2018 9:56 am
Location: Northamptonshire

Re: Counting T states in Excel

Post by arkannoyed »

djnzx48 wrote: Tue May 21, 2019 10:52 am Is this it? ticks.0.15.zip
Thats the one, though I've not much of in idea for how to use it though?
User avatar
djnzx48
Manic Miner
Posts: 729
Joined: Wed Dec 06, 2017 2:13 am
Location: New Zealand

Re: Counting T states in Excel

Post by djnzx48 »

OK, I just found some basic instructions here:
https://github.com/z88dk/z88dk/wiki/Tool---ticks

Edit: I just read that apparently ticks does not support contention. So it may not be the best choice for accurate measurements.
User avatar
arkannoyed
Manic Miner
Posts: 435
Joined: Mon Feb 05, 2018 9:56 am
Location: Northamptonshire

Re: Counting T states in Excel

Post by arkannoyed »

Ah yes, good find, those might help
AndyC
Dynamite Dan
Posts: 1388
Joined: Mon Nov 13, 2017 5:12 am

Re: Counting T states in Excel

Post by AndyC »

Contention makes things harder on the Speccy because it isn't just about what instruction you are executing, but when you are executing it. The same instruction within a loop might not always take the same length of time. I can't say I've ever entirely got my head around the various descriptions of it.

Contrast this with say the Amstrad CPC where contention is universally applied and highly predictable, then it is really just a case of having a slightly different list of instruction timings to check against.
User avatar
RMartins
Manic Miner
Posts: 776
Joined: Thu Nov 16, 2017 3:26 pm

Re: Counting T states in Excel

Post by RMartins »

The Zero emulator has an interesting trace feature (but it does make the emulator a bit slower), which allows you to track the instructions that are executing, while keeping a log of them.

This has proven useful to check timings of loop code, since you can check the T-State count, on start, during stepping and on exit.
Post Reply