Package: z80count
z80countThis is a simple tool that parses Z80 assembler using regular expressions (I know!)
and adds comments to the code with the cycles used by the instruction.It needs testing and probably a proper Z80 parser, but it works for me and the
Z80 assembler syntax I use.Installz80countrequires Python 3.To install for production you can usepip:pip3 install --user z80countOr you can download the package fromz80count's refs tab, unpack and run:python3 setup.py installTo install for development run:git clone https://git.usebox.net/z80count
cd z80count
pip3 install -e ".[dev]"UsageYou can use it with:z80count  < file.asm > file_c.asmOr insidevimyou can::% !z80count -sWith-sthe tool adds a subtotal.By defaultz80countwill try to update comments replacing existing annotations.Comments added byz80countare aligned to the column given in the-c(--column) option (50 by default). By default the comments are aligned using
spaces, if you prefer tabs instead use the-toption. In order to compute the
paddingz80countassumes that aTABequals 8 spaces. Use the
option-Tto override this.Example:pushhlpopbcldhl,$5800lde,7.fade_out_all_loop0pushhlpushbchalt.fade_out_all_loop1lda,(hl)and7jrz,no_fade_all_inkdeca.no_fade_all_inkldd,alda,(hl)and$38jrz,no_fade_all_papersub8.no_fade_all_paperordldd,alda,(hl)and$c0ordld(hl),ainchldecbclda,borcjrnz,fade_out_all_loop1popbcpophldecejrnz,fade_out_all_loop0Processed withz80count.py -sresults in:pushhl; [11 .. 11]popbc; [10 .. 21]ldhl,$5800; [10 .. 31]lde,7; [7 .. 38].fade_out_all_loop0pushhl; [11 .. 49]pushbc; [11 .. 60]halt; [4 .. 64].fade_out_all_loop1lda,(hl); [7 .. 71]and7; [7 .. 78]jrz,no_fade_all_ink; [12/7 .. 90/85]deca; [4 .. 89].no_fade_all_inkldd,a; [4 .. 93]lda,(hl); [7 .. 100]and$38; [7 .. 107]jrz,no_fade_all_paper; [12/7 .. 119/114]sub8; [7 .. 121].no_fade_all_paperord; [4 .. 125]ldd,a; [4 .. 129]lda,(hl); [7 .. 136]and$c0; [7 .. 143]ord; [4 .. 147]ld(hl),a; [7 .. 154]inchl; [6 .. 160]decbc; [6 .. 166]lda,b; [4 .. 170]orc; [4 .. 174]jrnz,fade_out_all_loop1; [12/7 .. 186/181]popbc; [10 .. 191]pophl; [10 .. 201]dece; [4 .. 205]jrnz,fade_out_all_loop0; [12/7 .. 217/212]Comments show subtotals, and there are two types:[A .. T0][B/A .. T1/T0]Where A, B, T0 and T1 are:A is the number of cycles of current instruction. In case of a conditional
instruction, this is the value when the condition is not met.B is the number of cycles of current instruction when the condition is met.T0 is the subtotal when the conditional is not met.T1 is the subtotal when the conditional is met.Config filez80countwill look for a config file in the following places, in order:the file given in the environment variableZ80COUNT_RC.a filez80countrcin the directory given in the environment variableXDG_DEFAULT_HOMEor, if this variable is undefined or empty, in
the directory~/.config.a file.z80countrcin the home directory.Example:[z80count]
# Column to align newly added comments
# column = 50

# Enable debug (show the matched case)
# debug = no

# Include subtotals
# subtotals = no

# Number of spaces for each tab
# tab width = 8

# Keep previous cycle annotations in the comment
# keep cycles = no

# Use tabs to align newly added comments instead of spaces
# use tabs = yesEditor supportz80count-el, emacsTroubleshootingHere be dragons!Use-dflag if you think one instruction is not correctly parsed.Feel free to open a PR if you find a bug!AuthorsJuan J. Martinezjjm@usebox.netAlexis Rodahttps://github.com/patxoca