/* all still very crude, but works */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>

__zpage long hptr;

long hadr=0x0ffd3640;

int hyppo_getversion() = "\tlda\t#0\n"
                         "\tsta\t[_hptr],z\n"
                         "\tclv\n"
                         "\tldz\t#0";

char hyppo_setname() = "\tinline\n"
                       "\tldy\t#$70\n"
                       "\tlda\t#$2e\n"
                       "\tsta\t[_hptr],z\n"
                       "\tclv\n"
                       "\tldz\t#0\n"
                       "\tbcc\t.err\n"
                       "\ttza\n"
                       ".err:\n"
                       "\tsta\t_errno\n"
                       "\teinline";

char hyppo_openfile() = "\tinline\n"
                        "\tlda\t#$18\n"
                        "\tsta\t[_hptr],z\n"
                        "\tclv\n"
                        "\tldz\t#0\n"
                        "\tbcs\t.noerr\n"
                        "\tsta\t_errno\n"
                        "\tlda\t#-1\n"
                        ".noerr:\n"
                        "\teinline";

char hyppo_findfile() = "\tinline\n"
                        "\tlda\t#$34\n"
                        "\tsta\t[_hptr],z\n"
                        "\tclv\n"
                        "\tldz\t#0\n"
                        "\tbcs\t.noerr\n"
                        "\tsta\t_errno\n"
                        "\tlda\t#-1\n"
                        ".noerr:\n"
                        "\teinline";




char hyppo_closefile(__reg("a") char) = "\tinline\n"
                                        "\ttax\n"
                                        "\tlda\t#$20\n"
                                        "\tsta\t[_hptr],z\n"
                                        "\tclv\n"
                                        "\tldz\t#0\n"
                                        "\teinline";


char hyppo_closeall() = "\tinline\n"
                        "\tlda\t#$22\n"
                        "\tsta\t[_hptr],z\n"
                        "\tclv\n"
                        "\tldz\t#0\n"
                        "\teinline";


int hyppo_readfile() = "\tinline\n"
                       "\tlda\t#$1a\n"
                       "\tsta\t[_hptr],z\n"
                       "\tclv\n"
                       "\tldz\t#0\n"
                       "\tbcs\t.noerr\n"
                       "\tsta\t_errno\n"
                       "\tldx\t#-1\n"
                       "\tldy\t#-1\n"
                       ".noerr:\n"
                       "\ttxa\n"
                       "\tpha\n"
                       "\ttya\n"
                       "\ttax\n"
                       "\tpla\n"
                       "\teinline";

char hyppo_loadfile(__reg("btmp0") long) = 
 "\tinline\n"
 "\tldx\tbtmp0\n"
 "\tldy\tbtmp0+1\n"
 "\tldz\t#0\n"
 "\tlda\t#$36\n"
 "\tsta\t[_hptr],z\n"
 "\tclv\n"
 "\tbcs\t.noerr\n"
 "\tlda\t#-1\n"
 "\tsta\t_errno\n"
 ".noerr:\n"
 "\tldz\t#0\n"
 "\teinline";

char hyppo_gofile(int) =
 "\tinline\n"
 "\tldx\tr0\n"
 "\tldy\tr1\n"
 "\tldz\t#0\n"
 "\tlda\t#$36\n"
 "\tjmp\t_zbuf\n"
 "_startcode:\n"
 "\tsta\t[_hptr],z\n"
 "\tclv\n"
 "\tbcs\t.noerr\n"
 "\tsta\t_errno\n"
 "\tjmp\t_gofailed\n"
 ".noerr:\n"
#if DEBUG
 "\tjmp\t.noerr\n"
#else
 "\tldz\t#0\n"
 "\tjmp\t(r0)\n"
#endif
 "\teinline";

static char readchar(__reg("btmp0") long) = "\tlda\t[btmp0],z";
static char writechar(__reg("a") char, __reg("btmp0") long) = "\tsta\t[btmp0],z";

__zpage char zbuf[20];

extern char startcode[];

void gofailed(void)
{
  printf("Failed loading file, errno=%d!\n",errno);
  exit(EXIT_FAILURE);
}

int main(int argc,char **argv)
{
 static char buf[10];
 char *s;
 long d=0;
 unsigned int i;

 printf("vbcc banked loader 0.1\n");

 hptr=hadr;
 i=hyppo_getversion();
 printf("hyppo version %u.%u\n",i&255,i>>8);
 //printf("errno=%d\n",errno);
 s=argv[0];
 if(s[0]=='0'&&s[1]==':') s+=2;
 if(argc>=2) s=argv[1];
 printf("Loading file from SD card: <%s>\n",s);

 strcpy((void*)0x7000,s);
 i=hyppo_setname();
 //printf("setnam = %d\n",i);
 //printf("errno=%d\n",errno);
#if 1
 //printf("copying cram and loading file\n");
 fflush(stdout);
 for(i=0;i<0x800;i++) writechar(readchar(0xff80000+i),0xff80800+i);
 writechar(8,0xffd3065);
 memcpy(&zbuf,&startcode,sizeof(zbuf));
 hyppo_gofile(0x2000);
#else
 i=hyppo_findfile();
 printf("findfile = %d\n",i);
 printf("errno=%d\n",errno);
 i=hyppo_openfile();
 printf("openfile = %d\n",i);
 printf("errno=%d\n",errno);
 do{
  unsigned int j;
  i=hyppo_readfile();
  printf("readfile = %d\n",i);
  printf("errno=%d\n",errno);
  if(d==0){
    d=readchar(0xffd6e00)|(readchar(0xffd6e01)<<8);
    printf("startaddress=%ld\n",d);
  }
  //hptr=0xffd6e00;
  for(j=0;j<i;j++){
    if((j&15)==0) {printf("\n%04X: ",j);gets(buf);}
    printf("%02X ",readchar(0xffd6e00+j));
  }
  printf("\n");
 }while(i>0);
 hyppo_closeall();
#endif

}

