Apple II monitor program

Compatible with Apple I monitor syntax and remote operation
This commit is contained in:
dschmenk 2013-05-30 11:50:59 -07:00
parent 96c93e6cd1
commit 79e8c35096
3 changed files with 201 additions and 42 deletions

View File

@ -87,18 +87,145 @@ int a2call(int fd, int address, int *result)
*result = (unsigned char)callpkt[1];
return ((unsigned char)callpkt[0] == 0x9E);
}
void prbytes(int address, int count, char *data)
{
int i;
printf("\n%04X:", address);
for (i = 0; i < count; i++)
printf(" %02X", (unsigned char)data[i]);
}
void exec(int fd, int cmd, int *address, int value, char *data, int *index)
{
int a, c;
switch (cmd)
{
case 0x00:
if (value != -1)
*address = value;
break;
case 0x90: /* read */
if (value < *address)
value = *address;
for (a = *address; a <= value; a += 16)
{
c = a + 16 > value ? value - a + 1 : 16;
a2read(fd, a, c, data);
prbytes(a, c, data);
}
*address = value + 1;
printf("\n");
break;
case 0x92: /* write */
if (*index)
a2write(fd, *address, *index, data);
*address += *index;
*index = 0;
printf("\n");
break;
case 0x94: /* call */
a2call(fd, *address, NULL);
printf("\n");
break;
}
}
int parsestr(int fd, char *cmdstr)
{
char databuf[1024];
static int addr = 0;
int index = 0;
int parseval = -1;
int cmd =0 ;
while (1)
{
switch (*cmdstr)
{
case ':': /* write bytes */
exec(fd, cmd, &addr, parseval, databuf, &index);
cmd = 0x92;
parseval = -1;
break;
case '.': /* read address range */
exec(fd, cmd, &addr, parseval, databuf, &index);
cmd = 0x90;
parseval = -1;
break;
case 'R': /* run */
case 'r':
exec(fd, cmd, &addr, parseval, databuf, &index);
cmd = 0x94;
break;
case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':
*cmdstr -= 'a' - 'A';
case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
case 'F':
*cmdstr -= 'A' - '9' - 1;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
if (parseval == -1)
parseval = 0;
parseval = parseval * 16 + *cmdstr - '0';
break;
case ' ':
if (parseval != -1)
{
if (cmd == 0x92)
databuf[index++] = parseval;
else
exec(fd, cmd, &addr, parseval, databuf, &index);
parseval = -1;
}
break;
case '\n':
case '\0':
if (parseval != -1 && cmd == 0x92)
databuf[index++] = parseval;
exec(fd, cmd, &addr, parseval, databuf, &index);
return 1;
break;
case 'Q':
case 'q':
return 0;
default:
return 1;
}
cmdstr++;
}
return 1;
}
int main(int argc, char **argv)
{
int pifd;
char databuf[1024];
pifd = a2open(argc > 1 ? argv[1] : "127.0.0.1");
char instr[256];
int pifd = a2open(argc > 1 ? argv[1] : "127.0.0.1");
if (pifd < 0)
{
perror("Unable to connect to Apple II Pi");
exit(EXIT_FAILURE);
}
a2call(pifd, 0xFBDD, NULL); // beep speaker
while (fgets(instr, 254, stdin) != NULL)
{
if (!parsestr(pifd, instr))
break;
}
a2close(pifd);
return EXIT_SUCCESS;
}

View File

@ -449,7 +449,6 @@ void finreq(int a2fd, int status, int result)
{
char finbuf[2];
struct a2request *a2req = a2reqlist;
printf("a2pid: finish request [0x%02X] [0x%02X]\n", status, result);
if (a2req->next)
{
/*
@ -485,7 +484,7 @@ void main(int argc, char **argv)
{
struct uinput_user_dev uidev;
struct termios oldtio,newtio;
char a2buf[16];
char iopkt[16];
int i, lastbtn;
int a2fd, kbdfd, moufd, srvfd, reqfd, maxfd;
struct sockaddr_in servaddr;
@ -615,13 +614,13 @@ void main(int argc, char **argv)
tcflush(a2fd, TCIFLUSH);
newtio.c_cc[VMIN] = 3; /* blocking read until 3 chars received */
tcsetattr(a2fd, TCSANOW, &newtio);
if (read(a2fd, a2buf, 1) == 1)
if (read(a2fd, iopkt, 1) == 1)
{
if (a2buf[0] == 0x80) /* receive sync */
if (iopkt[0] == 0x80) /* receive sync */
{
prlog("a2pid: Connected.\n");
a2buf[0] = 0x81; /* acknowledge */
write(a2fd, a2buf, 1);
iopkt[0] = 0x81; /* acknowledge */
write(a2fd, iopkt, 1);
tcflush(a2fd, TCIFLUSH);
}
else
@ -661,49 +660,49 @@ void main(int argc, char **argv)
*/
if (FD_ISSET(a2fd, &readset))
{
if (read(a2fd, a2buf, 3) == 3)
if (read(a2fd, iopkt, 3) == 3)
{
// printf("a2pi: Event [0x%02X] [0x%02X] [0x%02X]\n", a2buf[0], a2buf[1], a2buf[2]);
switch (a2buf[0])
// printf("a2pi: Event [0x%02X] [0x%02X] [0x%02X]\n", iopkt[0], iopkt[1], iopkt[2]);
switch (iopkt[0])
{
case 0x80: /* sync */
prlog("a2pid: Re-Connected.\n");
a2buf[0] = 0x81; /* acknowledge */
write(a2fd, a2buf, 1);
iopkt[0] = 0x81; /* acknowledge */
write(a2fd, iopkt, 1);
tcflush(a2fd, TCIFLUSH);
break;
case 0x82: /* keyboard event */
// printf("Keyboard Event: 0x%02X:%c\n", a2buf[1], a2buf[2] & 0x7F);
sendkey(kbdfd, a2buf[1], a2buf[2]);
if (a2buf[2] == 0x9B && a2buf[1] == 0xC0)
// printf("Keyboard Event: 0x%02X:%c\n", iopkt[1], iopkt[2] & 0x7F);
sendkey(kbdfd, iopkt[1], iopkt[2]);
if (iopkt[2] == 0x9B && iopkt[1] == 0xC0)
stop = TRUE;
break;
case 0x84: /* mouse move event */
// printf("Mouse XY Event: %d,%d\n", (signed char)a2buf[1], (signed char)a2buf[2]);
sendrelxy(moufd, accel[a2buf[1] & 0x0F], accel[a2buf[2] & 0x0F]);
// printf("Mouse XY Event: %d,%d\n", (signed char)iopkt[1], (signed char)iopkt[2]);
sendrelxy(moufd, accel[iopkt[1] & 0x0F], accel[iopkt[2] & 0x0F]);
break;
case 0x86: /* mouse button event */
// printf("Mouse Button %s Event 0x%02X\n", a2buf[2] ? "[PRESS]" : "[RELEASE]", a2buf[1]);
sendbttn(moufd, a2buf[1], a2buf[2]);
// printf("Mouse Button %s Event 0x%02X\n", iopkt[2] ? "[PRESS]" : "[RELEASE]", iopkt[1]);
sendbttn(moufd, iopkt[1], iopkt[2]);
break;
case 0x90: /* acknowledge read bytes request*/
if (a2reqlist) /* better have an outstanding request */
{
// printf("a2pid: read %d bytes from 0x%04X\n", a2reqlist->count, a2reqlist->addr);
newtio.c_cc[VMIN] = 1; /* blocking read until 1 char received */
tcsetattr(a2fd, TCSANOW, &newtio);
if (writeword(a2fd, a2reqlist->addr, 0x91) && writeword(a2fd, a2reqlist->count, 0x91))
{
for (i = 0; i < a2reqlist->count; i++)
{
if (read(a2fd, a2buf, 1) == 1)
a2reqlist->buffer[i] = a2buf[0];
if (read(a2fd, iopkt, 1) == 1)
a2reqlist->buffer[i] = iopkt[0];
else
{
stop = TRUE;
break;
}
}
// printf("a2pid: read %d bytes from 0x%04X\n", a2reqlist->count, a2reqlist->addr);
}
else
stop = TRUE;
@ -716,14 +715,13 @@ void main(int argc, char **argv)
case 0x92: /* acknowledge write bytes */
if (a2reqlist) /* better have an outstanding request */
{
// printf("a2pid: wrote %d bytes to 0x%04X\n", a2reqlist->count, a2reqlist->addr);
newtio.c_cc[VMIN] = 1; /* blocking read until 1 char received */
tcsetattr(a2fd, TCSANOW, &newtio);
if (writeword(a2fd, a2reqlist->addr, 0x93) && writeword(a2fd, a2reqlist->count, 0x93))
{
if (write(a2fd, a2reqlist->buffer, a2reqlist->count) != a2reqlist->count)
stop = TRUE;
// else
// printf("a2pid: wrote %d bytes to 0x%04X\n", a2reqlist->count, a2reqlist->addr);
}
else
stop = TRUE;
@ -736,12 +734,11 @@ void main(int argc, char **argv)
case 0x94: /* acknowledge call */
if (a2reqlist) /* better have an outstanding request */
{
// printf("a2pid: call address 0x%04X\n", a2reqlist->addr);
newtio.c_cc[VMIN] = 1; /* blocking read until 1 char received */
tcsetattr(a2fd, TCSANOW, &newtio);
if (!writeword(a2fd, a2reqlist->addr, 0x95))
stop = TRUE;
// else
// printf("a2pid: call address 0x%04X\n", a2reqlist->addr);
newtio.c_cc[VMIN] = 3; /* blocking read until 3 chars received */
tcsetattr(a2fd, TCSANOW, &newtio);
}
@ -750,7 +747,8 @@ void main(int argc, char **argv)
break;
case 0x9E: /* request complete ok */
case 0x9F: /* request complete error */
finreq(a2fd, (unsigned char)a2buf[0], (unsigned char)a2buf[1]);
// printf("a2pid: finish request 0x%02X:0x%02X\n", (unsigned char)iopkt[0], (unsigned char)iopkt[1]);
finreq(a2fd, (unsigned char)iopkt[0], (unsigned char)iopkt[1]);
break;
default:
prlog("a2pid: Unknown Event\n");
@ -786,16 +784,16 @@ void main(int argc, char **argv)
{
int addr, count;
char *databuf;
if (read(reqfd, a2buf, 1) == 1)
if (read(reqfd, iopkt, 1) == 1)
{
// printf("a2pi: Request [0x%02X]\n", a2buf[0]);
switch (a2buf[0])
// printf("a2pi: Request [0x%02X]\n", iopkt[0]);
switch (iopkt[0])
{
case 0x90: /* read bytes */
if (read(reqfd, a2buf, 4) == 4)
if (read(reqfd, iopkt, 4) == 4)
{
addr = (unsigned char)a2buf[0] | ((unsigned char)a2buf[1] << 8);
count = (unsigned char)a2buf[1] | ((unsigned char)a2buf[2] << 8);
addr = (unsigned char)iopkt[0] | ((unsigned char)iopkt[1] << 8);
count = (unsigned char)iopkt[2] | ((unsigned char)iopkt[3] << 8);
if (count)
{
databuf = malloc(count);
@ -804,10 +802,10 @@ void main(int argc, char **argv)
}
break;
case 0x92: /* write bytes */
if (read(reqfd, a2buf, 4) == 4)
if (read(reqfd, iopkt, 4) == 4)
{
addr = (unsigned char)a2buf[0] | ((unsigned char)a2buf[1] << 8);
count = (unsigned char)a2buf[1] | ((unsigned char)a2buf[2] << 8);
addr = (unsigned char)iopkt[0] | ((unsigned char)iopkt[1] << 8);
count = (unsigned char)iopkt[2] | ((unsigned char)iopkt[3] << 8);
if (count)
{
databuf = malloc(count);
@ -817,9 +815,9 @@ void main(int argc, char **argv)
}
break;
case 0x94: /* call */
if (read(reqfd, a2buf, 2) == 2)
if (read(reqfd, iopkt, 2) == 2)
{
addr = (unsigned char)a2buf[0] | ((unsigned char)a2buf[1] << 8);
addr = (unsigned char)iopkt[0] | ((unsigned char)iopkt[1] << 8);
addreq(a2fd, reqfd, 0x94, addr, 0, NULL);
}
break;

34
src/bintomon.c Executable file
View File

@ -0,0 +1,34 @@
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
FILE *obj;
int start_addr;
unsigned char b;
if (argc < 2)
{
fprintf(stderr, "Include start address on command line.\n");
exit(1);
}
if (argc < 3)
{
fprintf(stderr, "Include file to convert on command line.\n");
exit(1);
}
sscanf(argv[1], "%x", &start_addr);
if (obj = fopen(argv[2], "rb"))
{
printf("%04X:", start_addr);
while (fread(&b, 1, 1, obj) == 1)
{
printf(" %02X", b);
if (!(++start_addr & 0x07))
printf("\r\n:");
}
printf("\r\n");
fclose(obj);
}
return (0);
}