Subversion Repositories NedoOS

Rev

Rev 2272 | Blame | Compare with Previous | Last modification | View Log | Download

  1. memorystreamloadfile
  2. ;de = file name
  3. ;out: zf=1 if successful, zf=0 otherwise
  4. ;configurable params:
  5. ; .pagestoload <= MEMORYSTREAMMAXPAGES
  6. ; .errormask = 0xff to require loading the entire file into memory, 0x00 if only [pagestoload] needed
  7. ;ON_DATA_LOADED_CALLBACK defines label called upon every page load
  8.         call openstream_file
  9.         or a
  10.         ld a,MEMORYSTREAMERROR_FILEIO
  11.         ld (memorystreamerrorcode),a
  12.         ret nz
  13.         ld hl,0
  14.         ld de,hl
  15.         ld c,l
  16. .pagestoload=$+1
  17.         ld b,MEMORYSTREAMMAXPAGES
  18. .loadloop
  19.         ld a,c
  20.         ld (.pageindex),a
  21.         inc c ;increase page count
  22.         push bc
  23.         push de
  24.         push hl
  25.         OS_NEWPAGE
  26.         or a
  27.         jr z,.pageallocated
  28.         pop hl
  29.         pop de
  30.         pop bc
  31.         dec c ;decrease page count because there's no page
  32.         ld a,MEMORYSTREAMERROR_OOM
  33.         ld (memorystreamerrorcode),a
  34.         jr .breakloop
  35. .pageallocated
  36. .pageindex=$+1
  37.         ld a,0
  38.         add a,memorystreampages%256
  39.         ld l,a
  40.         adc a,memorystreampages/256
  41.         sub l
  42.         ld h,a
  43.         ld (hl),e
  44.         inc hl
  45.         ld (memorystreampageaddr),hl
  46.         ld a,e
  47.         ld (memorystreamcurrentpage),a
  48.         SETPG8000
  49.         ld de,0x8000
  50.         ld hl,0x4000
  51.         call readstream_file
  52.         ex (sp),hl
  53.         pop bc
  54.         pop de
  55.         add hl,bc
  56.         jr nc,$+3
  57.         inc e
  58.         ld a,b
  59.         pop bc
  60.         ifdef ON_DATA_LOADED_CALLBACK
  61.         push hl,de,bc,af
  62.         res 6,h
  63.         set 7,h
  64.         and 0x40
  65.         jr z,$+5
  66.         ld hl,0xc000
  67.         ld (memorystreamcurrentaddr),hl
  68.         call ON_DATA_LOADED_CALLBACK
  69.         pop bc
  70.         ld a,b
  71.         pop bc,de,hl
  72.         jr nz,.breakloop
  73.         endif
  74.         and 0x40
  75.         jr z,.breakloop
  76.         djnz .loadloop
  77. .errormask=$+1
  78.         and MEMORYSTREAMERRORMASK
  79. .breakloop
  80.         push af
  81.         ld (memorystreamsize+0),hl
  82.         ld (memorystreamsize+2),de
  83.         ld a,c
  84.         ld (memorystreampagecount),a
  85.         call closestream_file
  86.         pop af
  87.         jp nz,memorystreamfree
  88.         ld a,MEMORYSTREAMERROR_SUCCESS
  89.         ld (memorystreamerrorcode),a
  90.         ret
  91.  
  92. memorystreamfree
  93. ;out: zf=0 so that this function can be used to return error condition
  94. memorystreampagecount=$+1
  95.         ld a,0
  96.         call memorystreamfreecustompagecount
  97.         or 1
  98.         ret
  99.  
  100. memorystreamfreecustompagecount
  101. ;a = page count
  102. ;UNUSED_PAGE_ADDR defines address containing page index used to mark the pages that are already released
  103.         or a
  104.         ret z
  105.         ld b,a
  106.         ld hl,memorystreampages
  107. .pagefreeloop
  108.         ld e,(hl)
  109.         ifdef UNUSED_PAGE_ADDR
  110.         ld a,(UNUSED_PAGE_ADDR)
  111.         cp e
  112.         jr z,.alreadydeleted
  113.         ld (hl),a
  114.         endif
  115.         push bc
  116.         push hl
  117.         OS_DELPAGE
  118.         pop hl
  119.         pop bc
  120. .alreadydeleted
  121.         inc hl
  122.         djnz .pagefreeloop
  123.         ret
  124.  
  125. memorystreamstart
  126.         ld hl,0xffff
  127.         ld (memorystreamcurrentaddr),hl
  128.         ld hl,memorystreampages
  129.         ld (memorystreampageaddr),hl
  130.         ret
  131.  
  132. memorystreamnextpage
  133. memorystreampageaddr=$+1
  134.         ld hl,0
  135.         push af
  136.         ld a,(hl)
  137.         inc hl
  138.         ld (memorystreamcurrentpage),a
  139.         ld (memorystreampageaddr),hl
  140.         push bc
  141.         SETPG8000
  142.         pop bc
  143.         pop af
  144.         ld hl,0x8000
  145.         ret
  146.  
  147. memorystreamskip
  148. ;b = byte count
  149.         ld hl,(memorystreamcurrentaddr)
  150. .loop   bit 6,h
  151.         call nz,memorystreamnextpage
  152.         inc hl
  153.         djnz .loop
  154.         ld (memorystreamcurrentaddr),hl
  155.         ret
  156.  
  157.         macro memory_stream_write_byte src
  158.         bit 6,h
  159.         call nz,memorystreamnextpage
  160.         ld (hl),src
  161.         inc hl
  162.         endm
  163.  
  164.         macro memory_stream_read_byte dest
  165.         bit 6,h
  166.         call nz,memorystreamnextpage
  167.         ld dest,(hl)
  168.         inc hl
  169.         endm
  170.  
  171.         macro memory_stream_read_1 dst
  172.         ld hl,(memorystreamcurrentaddr)
  173.         memory_stream_read_byte dst
  174.         ld (memorystreamcurrentaddr),hl
  175.         endm
  176.  
  177.         macro memory_stream_read_2 dst1,dst2
  178.         ld hl,(memorystreamcurrentaddr)
  179.         memory_stream_read_byte dst1
  180.         memory_stream_read_byte dst2
  181.         ld (memorystreamcurrentaddr),hl
  182.         endm
  183.  
  184.         macro memory_stream_read_3 dst1,dst2,dst3
  185.         ld hl,(memorystreamcurrentaddr)
  186.         memory_stream_read_byte dst1
  187.         memory_stream_read_byte dst2
  188.         memory_stream_read_byte dst3
  189.         ld (memorystreamcurrentaddr),hl
  190.         endm
  191.  
  192. memorystreamread1
  193. ;out: a = byte
  194.         memory_stream_read_1 a
  195.         ret
  196.  
  197. memorystreamread2
  198. ;out: de = word
  199.         memory_stream_read_2 e,d
  200.         ret
  201.  
  202. memorystreamread3
  203. ;out: c = byte0, e = byte1, d = byte2
  204.         memory_stream_read_3 c,e,d
  205.         ret
  206.  
  207. memorystreamread4
  208. ;out: adbc = dword
  209. memorystreamcurrentaddr=$+1
  210.         ld hl,0
  211.         memory_stream_read_byte c
  212.         memory_stream_read_byte b
  213.         memory_stream_read_byte d
  214.         memory_stream_read_byte a
  215.         ld (memorystreamcurrentaddr),hl
  216.         ret
  217.  
  218. memorystreamread
  219. ;bc = number of bytes
  220. ;de = dest addr
  221.         ld a,c
  222.         dec bc
  223.         inc b
  224.         ld c,b
  225.         ld b,a
  226.         ld hl,(memorystreamcurrentaddr)
  227. .readloop
  228.         memory_stream_read_byte a
  229.         ld (de),a
  230.         inc de
  231.         djnz .readloop
  232.         dec c
  233.         jr nz,.readloop
  234.         ld (memorystreamcurrentaddr),hl
  235.         ret
  236.  
  237. memorystreamwrite
  238. ;bc = number of bytes
  239. ;de = src addr
  240.         ld a,c
  241.         dec bc
  242.         inc b
  243.         ld c,b
  244.         ld b,a
  245.         ld hl,(memorystreamcurrentaddr)
  246. .writeloop
  247.         ld a,(de)
  248.         memory_stream_write_byte a
  249.         inc de
  250.         djnz .writeloop
  251.         dec c
  252.         jr nz,.writeloop
  253.         ld (memorystreamcurrentaddr),hl
  254.         ret
  255.  
  256. memorystreamseek
  257. ;dehl = absolute position
  258. ;out: hl = read address
  259.         ld a,e
  260.         ld b,h
  261.         sla b
  262.         rla
  263.         sla b
  264.         rla
  265.         add a,memorystreampages%256
  266.         ld e,a
  267.         adc a,memorystreampages/256
  268.         sub e
  269.         ld d,a
  270.         ld a,(de)
  271.         ld (memorystreamcurrentpage),a
  272.         inc de
  273.         ld (memorystreampageaddr),de
  274.         SETPG8000
  275.         res 6,h
  276.         set 7,h
  277.         ld (memorystreamcurrentaddr),hl
  278.         ret
  279.  
  280. memorystreamgetpos
  281. ;out: dehl = absolute position
  282.         ld hl,(memorystreampageaddr)
  283.         ld de,-memorystreampages-1
  284.         add hl,de
  285.         ex de,hl
  286.         ld hl,(memorystreamcurrentaddr)
  287.         res 7,h
  288.         bit 6,h
  289.         jr z,$+6
  290.         inc de
  291.         ld hl,0
  292.         xor a
  293.         rr e
  294.         rra
  295.         rr e
  296.         rra
  297.         or h
  298.         ld h,a
  299.         ret
  300.  
  301. MEMORYSTREAMERROR_SUCCESS = 0
  302. MEMORYSTREAMERROR_FILEIO  = 1
  303. MEMORYSTREAMERROR_OOM     = 2
  304.  
  305. memorystreamsize
  306.         ds 4
  307. memorystreampages
  308.         ds MEMORYSTREAMMAXPAGES
  309. memorystreamcurrentpage
  310.         ds 1
  311. memorystreamerrorcode
  312.         ds 1
  313.