Counting T states in Excel
- Turtle_Quality
- Manic Miner
- Posts: 502
- Joined: Fri Dec 07, 2018 10:19 pm
Counting T states in Excel
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
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
- Turtle_Quality
- Manic Miner
- Posts: 502
- Joined: Fri Dec 07, 2018 10:19 pm
Re: Counting T states in Excel
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
Let's try again with a link to the zip
https://drive.google.com/open?id=1rdx4g ... qIPnsCDTWx
Definition of loop : see loop
- Ast A. Moore
- Rick Dangerous
- Posts: 2640
- Joined: Mon Nov 13, 2017 3:16 pm
Re: Counting T states in Excel
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.
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.
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.
Re: Counting T states in Excel
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:
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.
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
XOR A
LD H,A
ADD HL,HL
ADD HL,HL
ADD HL,HL
LD BC,&3C00
ADD HL, BC
- arkannoyed
- Manic Miner
- Posts: 435
- Joined: Mon Feb 05, 2018 9:56 am
- Location: Northamptonshire
Re: Counting T states in Excel
Provided you are only printing the usual range of characters from 00 to 7f then you can do it even shorter and faster;
37 t states only.
6 bytes
Code: Select all
Add a,a
Ld l,a
Ld h,0f
Add hl,hl
Add hl,hl
6 bytes
- Ast A. Moore
- Rick Dangerous
- Posts: 2640
- Joined: Mon Nov 13, 2017 3:16 pm
Re: Counting T states in Excel
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:
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.
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.
Re: Counting T states in Excel
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.
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.
- Turtle_Quality
- Manic Miner
- Posts: 502
- Joined: Fri Dec 07, 2018 10:19 pm
Re: Counting T states in Excel
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
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
Re: Counting T states in Excel
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.
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.
Re: Counting T states in Excel
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.
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.
- arkannoyed
- Manic Miner
- Posts: 435
- Joined: Mon Feb 05, 2018 9:56 am
- Location: Northamptonshire
Re: Counting T states in Excel
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.
Re: Counting T states in Excel
Is this it? ticks.0.15.zip
- Ast A. Moore
- Rick Dangerous
- Posts: 2640
- Joined: Mon Nov 13, 2017 3:16 pm
Re: Counting T states in Excel
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.
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.
- arkannoyed
- Manic Miner
- Posts: 435
- Joined: Mon Feb 05, 2018 9:56 am
- Location: Northamptonshire
Re: Counting T states in Excel
Thats the one, though I've not much of in idea for how to use it though?
Re: Counting T states in Excel
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.
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.
- arkannoyed
- Manic Miner
- Posts: 435
- Joined: Mon Feb 05, 2018 9:56 am
- Location: Northamptonshire
Re: Counting T states in Excel
Ah yes, good find, those might help
Re: Counting T states in Excel
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.
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.
Re: Counting T states in Excel
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.
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.