Counting T states in Excel

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

Counting T states in Excel

Post by Turtle_Quality » Mon May 20, 2019 6:50 pm

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)



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
0 x
Definition of loop : see loop

User avatar
Turtle_Quality
Berk
Posts: 48
Joined: Fri Dec 07, 2018 10:19 pm

Re: Counting T states in Excel

Post by Turtle_Quality » Mon May 20, 2019 6:53 pm

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

0 x
Definition of loop : see loop

User avatar
Ast A. Moore
Dynamite Dan
Posts: 1224
Joined: Mon Nov 13, 2017 3:16 pm

Re: Counting T states in Excel

Post by Ast A. Moore » Mon May 20, 2019 6:57 pm

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.
0 x
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: 568
Joined: Wed Dec 06, 2017 2:13 am
Location: New Zealand

Re: Counting T states in Excel

Post by djnzx48 » Mon May 20, 2019 11:53 pm

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:
SpoilerShow
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.
0 x

User avatar
arkannoyed
Manic Miner
Posts: 366
Joined: Mon Feb 05, 2018 9:56 am

Re: Counting T states in Excel

Post by arkannoyed » Tue May 21, 2019 7:20 am

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
0 x

User avatar
Ast A. Moore
Dynamite Dan
Posts: 1224
Joined: Mon Nov 13, 2017 3:16 pm

Re: Counting T states in Excel

Post by Ast A. Moore » Tue May 21, 2019 8:13 am

djnzx48 wrote:
Mon May 20, 2019 11:53 pm
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
0 x
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: 568
Joined: Wed Dec 06, 2017 2:13 am
Location: New Zealand

Re: Counting T states in Excel

Post by djnzx48 » Tue May 21, 2019 8: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. 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.
0 x

User avatar
Turtle_Quality
Berk
Posts: 48
Joined: Fri Dec 07, 2018 10:19 pm

Re: Counting T states in Excel

Post by Turtle_Quality » Tue May 21, 2019 8:33 am

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
0 x
Definition of loop : see loop

Ralf
Dynamite Dan
Posts: 1314
Joined: Mon Nov 13, 2017 11:59 am
Location: Poland

Re: Counting T states in Excel

Post by Ralf » Tue May 21, 2019 9:19 am

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.
0 x

User avatar
djnzx48
Manic Miner
Posts: 568
Joined: Wed Dec 06, 2017 2:13 am
Location: New Zealand

Re: Counting T states in Excel

Post by djnzx48 » Tue May 21, 2019 9:44 am

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.
1 x

Post Reply