Friday, February 19, 2016

History in the "right" order

I love the history command, but I can't handle the reverse sequential order.

So changing this in debugcmd.cpp

/*-------------------------------------------------
    execute_history - execute the history command
-------------------------------------------------*/

static void execute_history(running_machine &machine, int ref, int params, const char *param[])
{

...

           for (int index = (int) count - 1; index >= 0; index--)

//ORIGINAL for (int index = 0; index < (int) count; index++) 

all we have to do is make the index go backwards...
and if you want a segfault, just forget to specify the cpu number. boom goes the dynamite!

history ,10

Thursday, February 18, 2016

Adding a %c to printf

So looking at debugcmd.cpp there's a routine to do printf.


/*-------------------------------------------------
mini_printf - safe printf to a buffer
-------------------------------------------------*/

static int mini_printf(running_machine &machine, char *buffer, const char *format, int params, UINT64 *param)
{


and if I add this just underneath the case 'D'
    case 'c':
    if (params == 0)
    {
     debug_console_printf(machine, "Not enough parameters for format!\n");
     return 0;
    }
    p += sprintf(p, "%c", (UINT32)*param);
    param++;
    params--;
    break;

I can do this:


Perfecting the trace

I discovered how to print memory in the mame debugger.

If you type "help expressions" it gives you a nice rundown.

you can print

b@addr byte
w@addr word
d@addr doubleword
q@addr quadword

and you can prefix with (p=program, d=data, i=io) so pw@9921 will mean the word at program memory space 9921.

So now I've got a pretty good tracelog line:

trace trace0.txt,0,{tracelog "A=%02X EA=%04X HL=%04X DE=%04X BC=%04X SKIP=%1X 9921=%04X | ",a,ea,hl,de,bc,(psw&0x20)!=0,pw@9921}

and it generates:
A=00 EA=00D1 HL=C002 DE=000A BC=0090 SKIP=0 9921=0286  |  0008: EXX     
A=00 EA=00D1 HL=7B76 DE=0000 BC=0200 SKIP=0 9921=0286  |  0009: EXA     
A=CB EA=0286 HL=7B76 DE=0000 BC=0200 SKIP=0 9921=0286  |  000A: JMP     $9817
A=CB EA=0286 HL=7B76 DE=0000 BC=0200 SKIP=0 9921=0286  |  9817: JMP     $7A04
A=CB EA=0286 HL=7B76 DE=0000 BC=0200 SKIP=0 9921=0286  |  7A04: DMOV    EA,ECNT
A=CB EA=0685 HL=7B76 DE=0000 BC=0200 SKIP=0 9921=0286  |  7A06: DMOV    HL,EA
A=CB EA=0685 HL=0685 DE=0000 BC=0200 SKIP=0 9921=0286  |  7A07: SHLD    $9921
A=CB EA=0685 HL=0685 DE=0000 BC=0200 SKIP=0 9921=0685  |  7A0B: CALL    $7A34
A=CB EA=0685 HL=0685 DE=0000 BC=0200 SKIP=0 9921=0685  |  7A34: ONIW    VV:0F,$41
A=CB EA=0685 HL=0685 DE=0000 BC=0200 SKIP=0 9921=0685  |  7A37: RET     

Making IRR visible in the debugger

I thought, why not have the IRR visible in the debugger window. So adding the line

state_add( UPD7810_IRR, "IRR", m_irr).formatstr("%04X");

to upd7810.cpp

and

UPD7810_IRR to this enum in upd7810.h

enum
{
UPD7810_PC=1, UPD7810_SP, UPD7810_PSW,
UPD7810_EA, UPD7810_V, UPD7810_A, UPD7810_VA,
UPD7810_BC, UPD7810_B, UPD7810_C, UPD7810_DE, UPD7810_D, UPD7810_E, UPD7810_HL, UPD7810_H, UPD7810_L,
UPD7810_EA2, UPD7810_V2, UPD7810_A2, UPD7810_VA2,
...
UPD7810_LV0, UPD7810_LV1,
UPD7810_IRR
};


gives me this:



It shows up at the position you add it in the list in upd7810.cpp.

And one side effect of this is that now you can print this from the debugger, and use in tracelog:

print irr
printf "%04X",irr

Wednesday, February 17, 2016

More tables for ESC processing

For processing ESC 0 ($30) to ESC Z ($5A) there's a jump table at $140C.



So basically get the next char, put it into A.

If A is between $30 and $5A then jump to $13c2.



At $13c2, subtract $30 multiply by 2 and use that as the index into the jump table at $140c.



I made a crap bash script to generate the characters from $30 to $5a, directed it to a file and loaded it at $ff00 so I could open up another memory dump window and line it up all neatly for the screenshot.




Howabout that ubuntu high contrast screen theme. Pretty garish, but it makes for a little cleaner screenshot.

ok so let's look at the processing for ESC 0:





so for ESC 0 jump to $1743 which puts 27 into A and stores that in $982C. $982C holds the current line spacing in 1/216 in. 27/216 is 1/8th inch.



for ESC 1 jump to $1745 which puts 21 into A and stores that in $982C. 21/216 = 7/72 inch (why? for 7 bit systems)
for ESC 2 jump to $1747 which puts 36 into A and stores that in $982C. 36/216 = 1/6 inch

ESC 3 is kind of interesting where you set n/216 line spacing, jump to $174A, grab the next character and put that into $982C.


Fun fun fun!



ESC em processing in Action Printer 2000

So now that we've found that $13A3 is the ESC processing routine, let's do some disassembly.



000013A3: 9E                 CALT    ($00BC)  get next char
000013A4: 67 19              NEI     A,$19   is it cut sheet feeder mode
000013A6: CC                 JR      $13B3
000013A7: 77 7F              EQI     A,$7F   equal to 7F delete
000013A9: 27 1F              GTI     A,$1F   not equal to 7F, are we greater than 31
000013AB: 4E 25              JRE     $13D2   so less than or equal to 31 or equal to 7F jump to 13d2
000013AD: 27 2F              GTI     A,$2F    2F=/  decimal 47
000013AF: C3                 JR      $13B3   less than equal to 47 jump to 13b3
000013B0: 27 5A              GTI     A,$5A    greater than Z  5a is 90 = Z
000013B2: CF                 JR      $13C2   so here we are from 48 to 90


Anyway, let's not get confused with too many details. We'll get the next char and compare it to $19. $19 is the ascii code for EM (or end medium). If we are equal to $19 then jump to $13b3.

000013B3: 34 62 14           LXI     HL,$1462
000013B6: 1A                 MOV     B,A
000013B7: 2D                 LDAX    (HL+)
000013B8: 67 00              NEI     A,$00
000013BA: B8                 RET     
000013BB: 48 85              LDEAX   (HL++)
000013BD: 60 FA              EQA     A,B
000013BF: F7                 JR      $13B7
000013C0: 48 28              JEA     

and this code will scan through a table beginning at $1462 in a special format of 3 byte triplets. First byte is the ESC code, and the following two bytes are the address to jump to.



At the end of the table is a 00 at $14b3 which will stop the loop if we don't find our ESC code in the table and will execute the RET at $13ba.



So looking at the first triplet, it happens to be our entry for $19.



and it points to the address $175d.
0000175D: 9E                 CALT    ($00BC)   get next
0000175E: 67 30              NEI     A,$30      
00001760: C7                 JR      $1768       is next equal "0" jump to 1768
00001761: 77 34              EQI     A,$34       is it "4"
00001763: B8                 RET                 not 4 then return
00001764: 15 02 20           ORIW    VV:02,$20   set bit 5 of $9802
00001767: B8                 RET                 done
00001768: 05 02 DF           ANIW    VV:02,$DF   clear bit 5 of $9802
0000176B: B8                 RET                 done

which very neatly implements the ESC em command.



Now to investigate the rest of the table.

Tuesday, February 16, 2016

Processing control characters

So how does ap2000 process control characters like ESC?

I was watching the memory around $9800 and sending ESC M and ESC P would change the memory location $980a.



Typing trackmem enabled memory tracking and the relevant PC for $980a was $1510.



Setting a breakpoint on 1510 and then running history gave me enough info to figure out what was happening.




First it checks $C002. If the high bit is set, then there's data latched in $C003. So read that data and acknowledge by writing to $C004. You're looking at a history so it's in reverse chronological order (sequence goes from the bottom up).



Then we look at the byte, we check if it's less than 32 (nonprinting control characters) with LTI A,$20.

Eventually subtracting 7 and then shifting left (multiplying by 2), we get the offset into a jump table at $13DA.

SUINB A,$07 subract immediate value 7 and skip next if NB no borrow (result is greater than or equal to 0)
RET (skip this)
SLL A multiply by 2 with a shift left

LXI HL, $13DA put 13DA into HL

ADD L,A so now add A to L
ACI H,$00 add immediate with carry to H

LDEAX (HL) load EA indirect with the memory address pointed to by HL

JEA jump to value in EA




We have our table at 13DA:



and at address $1402 ($13DA + ($1b - 7) * 2) is the pointer to the routine for processing ESC, $13A3.



It's interesting to see in the table that with control characters that are ignored, like $1C, the jump address is $14b6 (shows up in the table as B6 14 because low byte is first) which points to just a single RET instruction.