/* bvwelch 2 june 2004

   Modified for tables on 2005-April-08 by Javier Martinez
   
   usage: mkweb2 < web.html > web.jal
*/
#include <stdio.h>
main()
{
  int c;

      printf("  var byte webcall_hi , webcall_lo \n");
      printf("\n");
      printf("-- *connection* variables. If you change this, \n");
      printf("-- you *MUST* adjust *FILE_CONN* in ram.jal\n");
      printf("const conn_fileoffset_lo = 0\n");
      printf("const conn_fileoffset_hi = 1\n");
      printf("\n");
      printf("  -- Change the return address in top of stack\n");
      printf("  -- add the offset (webcall_hi:lo) to the return address\n");
      printf("  -- located in TOS (Top Of Stack)\n");
      printf(" procedure _web_call_function is\n");
      printf("    assembler\n");
      printf("       movf   webcall_lo,w\n");
      printf("       addwf    TOSL , F\n");
      printf("       movf   webcall_hi,w\n");
      printf("       addwfc    TOSH , F\n");
      printf("       btfsc    STATUS_C\n");
      printf("       incf     TOSU , F\n");
      printf("       return\n");
      printf("    end assembler\n");
      printf("  end procedure\n");
      printf("\n");
      printf("\n");
      printf(" -- internal routine, returns code corresponding to value in w\n");
      printf(" function _webtable (byte in webtable_hi , byte in webtable_lo ) return byte is\n");
      printf("       assembler      -- convert *word* JAL address into *byte* PIC18 address\n");
      printf("          bcf status_c\n");
      printf("          rlf webtable_lo , w\n");
      printf("          movwf webcall_lo\n");
      printf("          rlf webtable_hi , w\n");
      printf("          movwf webcall_hi\n");
      printf("          call _web_call_function     -- Add webtable_hi:lo to return address\n");
      printf(" ; Commands table \n");

  do {
        c = getchar();
        if (c == EOF) break;
        if (c == 13) continue;
        if (c == '\n') {
          printf("retlw 10 ; new line\n");
        } else if (c == '"') {
          printf("retlw %d\n", c);
        } else {
          printf("retlw \"%c\"\n", c);
        }
  } while ( c != EOF ) ;
      printf("retlw 03  ; end of file\n");
      printf("      end assembler \n");
      printf(" end function \n");
      printf("\n");
      printf("function web_sector ( byte in file_ptr , byte in buff_ptr ) return bit is\n");
      printf("var byte hi , lo , counthi , countlo , webdata\n");
      printf("      counthi = 0                                              -- basic counter\n");
      printf("      countlo = 0\n");
      printf("      LoadPointer_2 ( buff_ptr )                        -- Init buffer\n");
      printf("      lo = MemRdEx ( file_ptr , conn_fileoffset_lo )         -- get the current file_offset count\n");
      printf("      hi = MemRdEx ( file_ptr , conn_fileoffset_hi )\n");
      printf("      while ! ( ( counthi == ( tcp_maxsegsize  >> 8 ) ) & ( countlo == ( tcp_maxsegsize & 0x00FF ) ) ) loop      -- loop for tcp_maxsegsize bytes\n");
      printf("           webdata =  _webtable ( hi , lo )           -- get data\n");
      printf("           if webdata == 03   then                                 -- end of file\n");
      printf("               MemWrEx ( file_ptr , conn_fileoffset_lo , countlo )    -- return in offset the size of buffer\n");
      printf("               MemWrEx ( file_ptr , conn_fileoffset_hi , counthi ) \n");
      printf("               return low                                                   -- end of file, size in offset\n");
      printf("           end if\n");
      printf("           MemNext2 = webdata                        -- put data in buffer\n");
      printf("           assembler                                           -- inc hi:lo and counthi:lo\n");
      printf("                incf     lo,f\n");
      printf("                btfsc  status_c\n");
      printf("                incf     hi,f\n");
      printf("                incf     countlo,f\n");
      printf("                btfsc  status_c\n");
      printf("                incf     counthi,f\n");
      printf("           end assembler    \n");
      printf("      end loop\n");
      printf("      MemWrEx ( file_ptr , conn_fileoffset_lo , lo )                      -- else points to next byte\n");
      printf("      MemWrEx ( file_ptr , conn_fileoffset_hi , hi ) \n");
      printf("      return true                                                           -- 512 bytes readed\n");
      printf("end function\n");

}
