Subversion Repositories NedoOS

Rev

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