Sunday, February 21, 2016

Driving the cr stepper motor

$C008 is where the action happens for driving the cr (carriage) stepper motor.

so running 'grep "$C008" ap2k_disasm' gives me


~/Downloads/mame$ grep '$C008' ap2k_disasm
000077B6: 70 79 08 C0 MOV ($C008),A
000077CE: 70 79 08 C0 MOV ($C008),A
00007851: 70 69 08 C0 MOV A,($C008)
00007B51: 70 69 08 C0 MOV A,($C008)
00007B5A: 70 79 08 C0 MOV ($C008),A
00007B5F: 70 79 08 C0 MOV ($C008),A

so we'll investigate these addresses.





Here it looks like some setup code around $77B4 writing zeroes to $C008 and then eventually initializing $C008 with $0d.

There's a table base address that gets put into $991D and $991E with LXI HL, $7B76 (load HL with the immediate value $7B76) and SHLD $991d (store HL direct to memory)



This table gets used later where at $7B4D, we LHLD $991D, which gets the base address of the table.

We get the current phase and put it into A with MOV A,($C008)

then LDAX (HL+A) gets the value for the next phase
and MOV ($C008),A writes it into $C008.

But if you look closely at our table at $7B66 and $7B76 the values look strange and the sequence is different.



The sequence runs for $7B66:

D5FBE6C8D

and for the reverse direction at $7B76:

D8C6EBF5D

and if you look at the code in e05a30.cpp, there's a difference: there's a SLA7020M that is actually driving the cr stepper. So we translate to the format that the SLA7020M wants.



So for fun I wanted to see exactly what this function would output: (hope this hilite.me thing works)

#include <stdio.h>
#include <string.h>
typedef int bool;

#define BIT(data,bit) (data&(1<<bit))

static int cr_sla7020m(int data)
{
	bool ina = BIT(data, 0);
	bool inb = BIT(data, 1);
	bool tda = BIT(data, 2);
	bool tdb = BIT(data, 3);
	bool outa0 =  ina && tda;
	bool outa1 = !ina && tda;
	bool outb0 =  inb && tdb;
	bool outb1 = !inb && tdb;
	return (outb1<<3)|(outb0<<2)|(outa1<<1)|(outa0<<0);
}



int hexchar2int(thischar){
if (thischar >= '0' && thischar <= '9') return thischar - '0';
else if (thischar >= 'A' && thischar <= 'F') return thischar - 'A'+10;
else printf("woo hoo");
}


void main (int argc, char *argv[]){

int i;
printf("translating 0 to 15\n");
for (i=0;i<16;i++) printf("%x %x\n",i,cr_sla7020m(i));


char * dostr = "D5FBE6C8D";
printf ("translating %s:\n",dostr);
for (i=0;i<strlen(dostr);i++){printf("%c %d -> %x \n",dostr[i],hexchar2int(dostr[i]),cr_sla7020m(hexchar2int(dostr[i])));}

char * dostr2 = "D8C6EBF5D";
printf ("translating %s:\n",dostr2);
for (i=0;i<strlen(dostr2);i++){printf("%c %d -> %x\n",dostr2[i],hexchar2int(dostr2[i]),cr_sla7020m(hexchar2int(dostr2[i])));}



}





~/Downloads/mame$ cc test_cr_sla_works.c
~/Downloads/mame$ ./a.out
translating 0 to 15
0 0
1 0
2 0
3 0
4 2
5 1
6 2
7 1
8 8
9 8
a 4
b 4
c a
d 9
e 6
f 5
translating D5FBE6C8D:
D 13 -> 9
5 5 -> 1
F 15 -> 5
B 11 -> 4
E 14 -> 6
6 6 -> 2
C 12 -> a
8 8 -> 8
D 13 -> 9
translating D8C6EBF5D:
D 13 -> 9
8 8 -> 8
C 12 -> a
6 6 -> 2
E 14 -> 6
B 11 -> 4
F 15 -> 5
5 5 -> 1
D 13 -> 9

and sure enough, this matches the sequence for the regular "NOT_A_REEL" stepper motor once filtered through the cr_sla7020m function.

No comments:

Post a Comment