Subversion Repositories ngs

Rev

Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. ; Part of NeoGS project, (c) NedoPC 2007-2008
  2. ;
  3. ; FPGA boot
  4. ;
  5. ;
  6.         include "cpld_ports.inc"
  7.  
  8.         relaxed on
  9.  
  10.                 ORG     0
  11.                 phase   0
  12.  
  13. stack_top       equ     0x4080
  14. recode_tbl      equ     0x4100
  15.  
  16. wbuf_addr       equ     0x6000 ;8kB buffer, 8kb boundary
  17. wbuf_size       equ     0x2000
  18. wbuf_hsize      equ     wbuf_size>>8
  19. wbuf_mask       equ     1fffh
  20. wbuf_hmask      equ     wbuf_mask>>8
  21. wbuf_hend       equ     (wbuf_addr+wbuf_size)>>8
  22.  
  23.  
  24.  
  25.                 di
  26.                 di
  27.                 jr      LOAD
  28.  
  29.                 jp      (ix)
  30.                 jp      (ix)
  31.  
  32.                 ret
  33.                 ret
  34.                 ret
  35.                 ret
  36.  
  37.                 di
  38.                 halt
  39.                 di
  40.                 halt
  41.  
  42. DEF_ROM:
  43.                 di      ;for default ROM load
  44.                 ld      sp,stack_top
  45.                 call    prepare
  46.                 jr      ROM
  47.  
  48. LOAD:
  49.                 ld      sp,stack_top ; use for stack first 128 bytes of RAM
  50.  
  51.                 call    prepare
  52.  
  53.  
  54.                 in      a,(C_CRES)
  55.                 rlca
  56.                 jr      c,TRY_RAM
  57. ROM:
  58.                 xor     a
  59.                 out     (C_MCFG),a
  60.                 ld      hl,config_data
  61.                 jr      loadconfig
  62.  
  63. TRY_RAM:
  64.                 ld      a,0x81
  65.                 out     (C_MCFG),a ;ram at 8000-FFFF, page 1
  66.  
  67.                 ld      hl,0x8000
  68.                 ld      b,(hl)
  69.                 inc     l
  70.                 ld      c,(hl)
  71.                 inc     l
  72.  
  73.                 ld      a,b
  74.                 or      a
  75.                 jr      z,ROM  ;$0100 min length!
  76.                 cp      0x7F
  77.                 jr      nc,ROM ;$7EFF max length!
  78.  
  79.                 add     hl,bc
  80.  
  81.                 ld      d,(hl) ;precomputed CRC value
  82.                 dec     (hl)    ;KILL CRC
  83.                 inc     hl
  84.                 ld      e,(hl)
  85.                 inc     (hl)    ;KILL CRC
  86.                 push    de
  87.  
  88.                 inc     bc
  89.                 inc     bc
  90.                 ld      hl,0x8000
  91.                 call    CRC16
  92.  
  93.                 pop     hl
  94.                 or      a
  95.                 sbc     hl,de
  96.                 jr      nz,ROM ;if CRC failed
  97.  
  98.                 ld      hl,0x8002
  99.  
  100.                 ;; start configuration
  101. loadconfig:
  102.                 ld      a,0x81
  103.                 out     (C_CRNCFG),a    ;clear cold_reset flag
  104.                 call    configure
  105.  
  106.                 ; end config, see is it correct
  107. wait_init:
  108.                 in      a,(C_NSTCDON)
  109.                 sra     a
  110.                 jp      p,DEF_ROM
  111.                 jr      nc,wait_init
  112.  
  113.                 ; here CONF_DONE is released, nothing to do more (reboot takes place when INIT_DONE toggles)
  114.  
  115.                 di
  116.                 halt
  117.  
  118.  
  119.  
  120. USE_BUFFER:     ;hl - from
  121.                 ;de - len (down to zero)
  122.                 ;kills af,bc,de,hl
  123.  
  124.                 ld      b,recode_tbl>>8
  125.                 jr      ub_entry
  126.  
  127. ub_loop:
  128.                 ld      c,(hl)
  129.                 inc     hl
  130.                 ld      a,(bc)
  131.                 out     (C_FPGADAT),a
  132.  
  133.  
  134.                 call    ready
  135.  
  136.  
  137.                 dec     de
  138.  
  139. ub_entry:       ld      a,d
  140.                 or      e
  141.                 jr      nz,ub_loop
  142.  
  143.                 ret
  144.  
  145.  
  146. prepare:
  147.                 ;reset FPGA by pulling NCONFIG down
  148.                 xor     a
  149.                 out     (C_CRNCFG),a
  150.  
  151.                 ;calc and compare CRC of rom
  152.                 ld      hl,0
  153.                 ld      bc,crc_is_here
  154.                 call    CRC16
  155.                 ld      a,(hl)
  156.                 inc     hl
  157.                 ld      l,(hl)
  158.                 ld      h,a
  159.                 or      a
  160.                 sbc     hl,de
  161.                 jr      nz,prepare
  162.  
  163.                 call    mk_recode_tbl
  164.  
  165.                 ld      a,1
  166.                 out     (C_CRNCFG),a ;release nconfig
  167.  
  168. wait_nstat:
  169.                 in      a,(C_NSTCDON) ;read nstatus, wait it to be 1
  170.                 rla
  171.                 jr      nc,wait_nstat
  172.  
  173.                 ret
  174.  
  175.  
  176. ready:
  177.  
  178. cfg_rdynbsy:    ; wait for ready
  179.                 in      a,(C_FPGADAT)
  180.                 rla
  181.                 jr      nc,cfg_rdynbsy
  182.  
  183.                 ; restart if error during configuration
  184.                 in      a,(C_NSTCDON)
  185.                 rla
  186.                 jp      nc,DEF_ROM
  187.  
  188.                 ret
  189.  
  190.  
  191.  
  192. mk_recode_tbl:
  193.                 ld      de,recode_tbl
  194. mrt_loop:
  195.  
  196.                 ld      a,e
  197.                 and     0b01001001 ; preserve d0,d3,d6
  198.                 ld      b,a
  199.  
  200.                 ld      a,e
  201.                 and     0b10010000 ; d7 and d4
  202.                 rrca
  203.                 rrca
  204.                 rrca
  205.                 or      b
  206.                 ld      b,a ;01011011
  207.  
  208.                 ld      a,e
  209.                 and     0b00000010 ; d1
  210.                 rlca
  211.                 or      b
  212.                 ld      b,a ;01011111
  213.  
  214.                 ld      a,e
  215.                 and     0b00000100 ; d2
  216.                 rlca
  217.                 rlca
  218.                 rlca
  219.                 or      b
  220.                 ld      b,a ;01111111
  221.  
  222.                 ld      a,e
  223.                 and     0b00100000 ; d5
  224.                 rlca
  225.                 rlca
  226.                 or      b ;11111111
  227.  
  228.                 ld      (de),a
  229.  
  230.                 inc     e
  231.                 jr      nz,mrt_loop
  232.  
  233.                 ret
  234.  
  235. ;; recode config data:
  236.  
  237. ;    Z80     fpga
  238. ;     D0       D0 *
  239. ;     D1       D4
  240. ;     D2       D1
  241. ;     D3       D3 *
  242. ;     D4       D7
  243. ;     D5       D2
  244. ;     D6       D6 *
  245. ;     D7       D5
  246.  
  247.  
  248.  
  249. configure:
  250.         ld      de,wbuf_addr
  251.  
  252. DEC40buf
  253.         LD      A,0x80
  254.         EX      AF,AF'
  255. MS      LDI
  256.         call    after_ldi
  257. M0      LD      BC,0x2FF
  258. M1      EX      AF,AF'
  259. M1X     ADD     A,A
  260.         JR      NZ,M2
  261.         LD      A,(HL)
  262.         INC     HL
  263.         RLA
  264. M2      RL      C
  265.         JR      NC,M1X
  266.         EX      AF,AF'
  267.        DJNZ    X2
  268.        LD      A,2
  269.        SRA     C
  270.        JR      C,N1
  271.        INC     A
  272.        INC     C
  273.        JR      Z,N2
  274.        LD      BC,0x33F
  275.        JR      M1
  276.  
  277. X2      DJNZ    X3
  278.        SRL     C
  279.        JR      C,MS
  280.        INC     B
  281.        JR      M1
  282. X6
  283.        ADD     A,C
  284. N2
  285.        LD      BC,0x4FF
  286.        JR      M1
  287. N1
  288.        INC     C
  289.        JR      NZ,M4
  290.        EX      AF,AF'
  291.         INC     B
  292. N5      RR      C
  293. ;;;;    RET     C
  294.         jr      c,dec40end
  295.  
  296.         RL      B
  297.         ADD     A,A
  298.         JR      NZ,N6
  299.         LD      A,(HL)
  300.         INC     HL
  301.         RLA
  302. N6      JR      NC,N5
  303.         EX      AF,AF'
  304.        ADD     A,B
  305.        LD      B,6
  306.        JR      M1
  307. X3
  308.        DJNZ    X4
  309.        LD      A,1
  310.        JR      M3
  311. X4      DJNZ    X5
  312.        INC     C
  313.        JR      NZ,M4
  314.        LD      BC,0x51F
  315.        JR      M1
  316. X5
  317.        DJNZ    X6
  318.        LD      B,C
  319. M4      LD      C,(HL)
  320.        INC     HL
  321. M3      DEC     B
  322.        PUSH    HL
  323.        LD      L,C
  324.        LD      H,B
  325.        ADD     HL,DE
  326.        LD      C,A
  327.        LD      B,0
  328. ;;;;    LDIR
  329.         call    wrap_ldir
  330.        POP     HL
  331.        JR      M0
  332. END_DEC40
  333.  
  334. dec40end:
  335.                 ex      de,hl
  336.                 ld      de,wbuf_addr
  337.                 or      a
  338.                 sbc     hl,de
  339.                 ex      de,hl
  340.                 jp      USE_BUFFER
  341.  
  342.  
  343. after_ldi:      push    af
  344.                 ld      a,d
  345.                 cp      wbuf_hend
  346.                 jr      c,al_nothing
  347.                 push    hl
  348.  
  349.                 ld      hl,wbuf_addr
  350.                 ld      de,0x2000
  351.                 call    USE_BUFFER
  352.  
  353.                 ld      de,wbuf_addr
  354.                 pop     hl
  355. al_nothing:
  356.                 pop     af
  357.                 ret
  358.  
  359.  
  360. wrap_ldir:
  361.                 ld      a,h
  362.                 cp      wbuf_addr>>8
  363.                 jr      nc,wl_nohlwrap
  364.                 add     a,wbuf_hsize
  365.                 ld      h,a
  366. wl_nohlwrap:
  367.                 ld      a,wbuf_hend-1
  368. wl_ldiloop:
  369.                 ldi
  370.                 jp      po,after_ldi
  371.  
  372.                 cp      d
  373.                 jr      c,wl_dewrap
  374.                 cp      h
  375.                 jr      nc,wl_ldiloop
  376. ;wl_hlwrap
  377.                 ld      h,wbuf_addr>>8
  378.                 jr      wl_ldiloop
  379. wl_dewrap:
  380.                 push    bc
  381.                 call    after_ldi
  382.                 pop     bc
  383.                 jr      wl_nohlwrap
  384.  
  385.  
  386.  
  387.  
  388. CRC16:  ;calculates crc16-ccitt with initial value of CRC=$FFFF
  389.  
  390.         ;INPUT:         HL - memptr
  391.         ;               BC  - byte count
  392.         ;OUTPUT:        DE - resulting crc value
  393.         ;               HL - ptr to byte after last processed
  394.         ;KILLS:         AF,BC,IX
  395.  
  396.                 ;algo: crc = (crc<<8) ^ crc16tab[((crc>>8) ^ *(char *)buf++)&0x00FF];
  397.  
  398.                 ld      a,c
  399.                 or      a
  400.                 jr      z,$+3
  401.                 inc     b
  402.  
  403.                 ld      ixl,b
  404.                 ld      b,c
  405.  
  406.  
  407.                 ex      de,hl
  408.                 ld      hl,(crc16table&$FF00)+$FF       ;high part of table, initial crc.lo
  409.                 ld      c,l                             ;initial crc.hi
  410.  
  411. c1_loop
  412.  
  413. ;de - memptr; h - table>>8, c - crc.hi, l - crc.lo, b - counter
  414.  
  415.                 ld      a,(de)
  416.                 inc     de
  417.                 xor     c
  418.                 ld      c,a     ;c=index
  419.  
  420.                 ld      a,l     ;a=crc.lo
  421.                 ld      l,c     ;hl=tab.hi
  422.                 xor     (hl)    ;a=new crc.hi
  423.                 inc     h
  424.                 ld      l,(hl)  ;l=new crc.lo
  425.                 dec     h
  426.                 ld      c,a
  427.  
  428.                 djnz    c1_loop
  429.  
  430.                 dec     ixl
  431.                 jr      nz,c1_loop
  432.  
  433.  
  434.                 ld      h,c
  435.                 ex      de,hl
  436.  
  437.                 ret
  438.  
  439.  
  440.  
  441.  
  442.  
  443.  
  444.  
  445.  
  446.  
  447.  
  448.  
  449.  
  450. config_data:
  451.                 binclude        "main.mlz"
  452.  
  453.                 org     ($+255)&0xFF00
  454. crc16table:
  455.         db      0x00,0x10,0x20,0x30,0x40,0x50,0x60,0x70
  456.         db      0x81,0x91,0xa1,0xb1,0xc1,0xd1,0xe1,0xf1
  457.         db      0x12,0x02,0x32,0x22,0x52,0x42,0x72,0x62
  458.         db      0x93,0x83,0xb3,0xa3,0xd3,0xc3,0xf3,0xe3
  459.         db      0x24,0x34,0x04,0x14,0x64,0x74,0x44,0x54
  460.         db      0xa5,0xb5,0x85,0x95,0xe5,0xf5,0xc5,0xd5
  461.         db      0x36,0x26,0x16,0x06,0x76,0x66,0x56,0x46
  462.         db      0xb7,0xa7,0x97,0x87,0xf7,0xe7,0xd7,0xc7
  463.         db      0x48,0x58,0x68,0x78,0x08,0x18,0x28,0x38
  464.         db      0xc9,0xd9,0xe9,0xf9,0x89,0x99,0xa9,0xb9
  465.         db      0x5a,0x4a,0x7a,0x6a,0x1a,0x0a,0x3a,0x2a
  466.         db      0xdb,0xcb,0xfb,0xeb,0x9b,0x8b,0xbb,0xab
  467.         db      0x6c,0x7c,0x4c,0x5c,0x2c,0x3c,0x0c,0x1c
  468.         db      0xed,0xfd,0xcd,0xdd,0xad,0xbd,0x8d,0x9d
  469.         db      0x7e,0x6e,0x5e,0x4e,0x3e,0x2e,0x1e,0x0e
  470.         db      0xff,0xef,0xdf,0xcf,0xbf,0xaf,0x9f,0x8f
  471.         db      0x91,0x81,0xb1,0xa1,0xd1,0xc1,0xf1,0xe1
  472.         db      0x10,0x00,0x30,0x20,0x50,0x40,0x70,0x60
  473.         db      0x83,0x93,0xa3,0xb3,0xc3,0xd3,0xe3,0xf3
  474.         db      0x02,0x12,0x22,0x32,0x42,0x52,0x62,0x72
  475.         db      0xb5,0xa5,0x95,0x85,0xf5,0xe5,0xd5,0xc5
  476.         db      0x34,0x24,0x14,0x04,0x74,0x64,0x54,0x44
  477.         db      0xa7,0xb7,0x87,0x97,0xe7,0xf7,0xc7,0xd7
  478.         db      0x26,0x36,0x06,0x16,0x66,0x76,0x46,0x56
  479.         db      0xd9,0xc9,0xf9,0xe9,0x99,0x89,0xb9,0xa9
  480.         db      0x58,0x48,0x78,0x68,0x18,0x08,0x38,0x28
  481.         db      0xcb,0xdb,0xeb,0xfb,0x8b,0x9b,0xab,0xbb
  482.         db      0x4a,0x5a,0x6a,0x7a,0x0a,0x1a,0x2a,0x3a
  483.         db      0xfd,0xed,0xdd,0xcd,0xbd,0xad,0x9d,0x8d
  484.         db      0x7c,0x6c,0x5c,0x4c,0x3c,0x2c,0x1c,0x0c
  485.         db      0xef,0xff,0xcf,0xdf,0xaf,0xbf,0x8f,0x9f
  486.         db      0x6e,0x7e,0x4e,0x5e,0x2e,0x3e,0x0e,0x1e
  487.  
  488.         db      0x00,0x21,0x42,0x63,0x84,0xa5,0xc6,0xe7
  489.         db      0x08,0x29,0x4a,0x6b,0x8c,0xad,0xce,0xef
  490.         db      0x31,0x10,0x73,0x52,0xb5,0x94,0xf7,0xd6
  491.         db      0x39,0x18,0x7b,0x5a,0xbd,0x9c,0xff,0xde
  492.         db      0x62,0x43,0x20,0x01,0xe6,0xc7,0xa4,0x85
  493.         db      0x6a,0x4b,0x28,0x09,0xee,0xcf,0xac,0x8d
  494.         db      0x53,0x72,0x11,0x30,0xd7,0xf6,0x95,0xb4
  495.         db      0x5b,0x7a,0x19,0x38,0xdf,0xfe,0x9d,0xbc
  496.         db      0xc4,0xe5,0x86,0xa7,0x40,0x61,0x02,0x23
  497.         db      0xcc,0xed,0x8e,0xaf,0x48,0x69,0x0a,0x2b
  498.         db      0xf5,0xd4,0xb7,0x96,0x71,0x50,0x33,0x12
  499.         db      0xfd,0xdc,0xbf,0x9e,0x79,0x58,0x3b,0x1a
  500.         db      0xa6,0x87,0xe4,0xc5,0x22,0x03,0x60,0x41
  501.         db      0xae,0x8f,0xec,0xcd,0x2a,0x0b,0x68,0x49
  502.         db      0x97,0xb6,0xd5,0xf4,0x13,0x32,0x51,0x70
  503.         db      0x9f,0xbe,0xdd,0xfc,0x1b,0x3a,0x59,0x78
  504.         db      0x88,0xa9,0xca,0xeb,0x0c,0x2d,0x4e,0x6f
  505.         db      0x80,0xa1,0xc2,0xe3,0x04,0x25,0x46,0x67
  506.         db      0xb9,0x98,0xfb,0xda,0x3d,0x1c,0x7f,0x5e
  507.         db      0xb1,0x90,0xf3,0xd2,0x35,0x14,0x77,0x56
  508.         db      0xea,0xcb,0xa8,0x89,0x6e,0x4f,0x2c,0x0d
  509.         db      0xe2,0xc3,0xa0,0x81,0x66,0x47,0x24,0x05
  510.         db      0xdb,0xfa,0x99,0xb8,0x5f,0x7e,0x1d,0x3c
  511.         db      0xd3,0xf2,0x91,0xb0,0x57,0x76,0x15,0x34
  512.         db      0x4c,0x6d,0x0e,0x2f,0xc8,0xe9,0x8a,0xab
  513.         db      0x44,0x65,0x06,0x27,0xc0,0xe1,0x82,0xa3
  514.         db      0x7d,0x5c,0x3f,0x1e,0xf9,0xd8,0xbb,0x9a
  515.         db      0x75,0x54,0x37,0x16,0xf1,0xd0,0xb3,0x92
  516.         db      0x2e,0x0f,0x6c,0x4d,0xaa,0x8b,0xe8,0xc9
  517.         db      0x26,0x07,0x64,0x45,0xa2,0x83,0xe0,0xc1
  518.         db      0x1f,0x3e,0x5d,0x7c,0x9b,0xba,0xd9,0xf8
  519.         db      0x17,0x36,0x55,0x74,0x93,0xb2,0xd1,0xf0
  520.  
  521. crc_is_here:
  522.  
  523.