home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 4 / DATAFILE_PDCD4.iso / utilities / utilsc / computils / !CompUtils / Resources / Compress / asm / s / Example
Encoding:
Text File  |  1996-04-01  |  4.9 KB  |  288 lines

  1. ; buffer sizes
  2. IBUFF    *    16*1024
  3. OBUFF    *    16*1024
  4.  
  5. ; define register bindings
  6. R0    RN    0
  7. R1    RN    1
  8. R2    RN    2
  9. R3    RN    3
  10. R4    RN    4
  11. R5    RN    5
  12. R6    RN    6
  13. R7    RN    7
  14. R8    RN    8
  15. R9    RN    9
  16. R10    RN    10
  17. R11    RN    11
  18. R12    RN    12
  19. R13    RN    13
  20. R14    RN    14
  21. R15    RN    15
  22. PC    RN    15
  23.  
  24. ; define SWIs (I hate ObjAsm!)
  25. OS_File            *    &08
  26. OS_Args            *    &09
  27. XOS_Args        *    OS_Args + (1:SHL:17)
  28. OS_GBPB            *    &0C
  29. XOS_GBPB        *    OS_GBPB + (1:SHL:17)
  30. OS_Find            *    &0D
  31. XOS_Find        *    OS_Find + (1:SHL:17)
  32. OS_GetEnv        *    &10
  33. OS_Exit            *    &11
  34. OS_GenerateError    *    &2B
  35.  
  36.     ; include header file
  37.     GET    CompUtils:Compress.asm.h.Type6
  38.     GET    CompUtils:Compress.asm.h.Universal
  39.  
  40. ; ==========================================================================
  41.  
  42.     AREA    MyCode,CODE,READONLY
  43.  
  44.     IMPORT    |Image$$ZI$$Limit|
  45.  
  46. start
  47.     ENTRY
  48.  
  49.     ; set up stack, workspace and workspace end
  50.     SWI    OS_GetEnv
  51.     MOV    R13,R1                 ; stack ptr
  52.     LDR    R12,=|Image$$ZI$$Limit|  ; workspace ptr
  53.     SUB    R11,R13,#1024         ; workspace limit
  54.     LDR    R6,=Compress_6         ; ptr to Compress
  55.     MOV    R7,#0             ; destination handle
  56.     MOV    R8,#0             ; src handle
  57.  
  58.     ; claim the source buffer
  59.     MOV    R0,#IBUFF
  60.     STR    R0,[R6,#Compress_SrcLength]
  61.     BL    claim
  62.     STR    R0,[R6,#Compress_SrcBufferPtr]
  63.  
  64.     ; claim the destination buffer
  65.     MOV    R0,#OBUFF
  66.     STR    R0,[R6,#Compress_DestLength]
  67.     BL    claim
  68.     STR    R0,[R6,#Compress_DestBufferPtr]
  69.  
  70.     ; claim the global workspace
  71.     LDR    R0,[R6,#Compress_GlobalLength]
  72.     BL    claim
  73.     STR    R0,[R6,#Compress_GlobalPtr]
  74.  
  75.     ; set up reason code
  76.     MOV    R0,#0
  77.     STR    R0,[R6,#Compress_ReasonCode]
  78.  
  79.     ; set up output flags
  80.     MOV    R0,#OUTPUT_LINEAR_6
  81.     STR    R0,[R6,#Compress_Flags]
  82.  
  83.     ; open source file
  84.     MOV    R0,#&4C
  85.     ADR    R1,infile
  86.     SWI    OS_Find
  87.     MOVS    R8,R0        ; input handle
  88.     ADREQ    R0,openinerror
  89.     SWIEQ    OS_GenerateError
  90.  
  91.     ; read sample length
  92.     MOV    R0,#2
  93.     MOV    R1,R8
  94.     SWI    XOS_Args
  95.     BVS    close_and_error
  96.     SUB    R2,R2,#1
  97.     STR    R2,[R6,#Compress_SampleLength]
  98.  
  99.     ; read sample period
  100.     MOV    R0,#4
  101.     MOV    R1,R8
  102.     ADD    R2,R6,#Compress_SamplePeriod
  103.     MOV    R3,#1
  104.     SWI    XOS_GBPB
  105.     BVS    close_and_error
  106.  
  107.     ; open destination file
  108.     MOV    R0,#&8C
  109.     ADR    R1,outfile
  110.     SWI    XOS_Find
  111.     BVS    close_and_error
  112.     MOVS    R7,R0        ; output handle
  113.     ADREQ    R0,openinerror
  114.     BEQ    close_and_error
  115.  
  116. mainloop
  117.     ; call Compress
  118.     STMFD    R13!,{R11,R12,R7,R8,R6}
  119.     BL    Compress_6_Code
  120.     LDMFD    R13!,{R11,R12,R7,R8,R6}
  121.  
  122.     ; process reason code
  123.     ; r0 = reason code
  124.     CMP    R0,#9
  125.     ADDCC    PC,PC,R0,LSL #2
  126.     BCS    dont_recognise_code
  127.     ; jump table
  128.     B    finished
  129.     B    src_empty
  130.     B    dest_full
  131.     B    scan_done
  132.     B    next_pass
  133.     B    dont_recognise_code ; src error - shouldn't occur
  134.     B    dont_recognise_code ; dest error - shouldn't occur
  135.     B    claim_work
  136.     B    unknown_format
  137.  
  138. src_empty
  139.     ; refill source buffer
  140.     MOV    R0,#4
  141.     MOV    R1,R8
  142.     LDR    R2,[R6,#Compress_SrcBufferPtr]
  143.     MOV    R3,#IBUFF
  144.     SWI    XOS_GBPB
  145.     BVS    close_and_error
  146.     B    mainloop
  147.  
  148. dest_full
  149.     ; output destination buffer
  150.     MOV    R0,#2
  151.     MOV    R1,R7
  152.     LDR    R2,[R6,#Compress_DestBufferPtr]
  153.     LDR    R3,[R6,#Compress_BytesWritten]
  154.     SWI    XOS_GBPB
  155.     BVS    close_and_error
  156.     B    mainloop
  157.  
  158. scan_done
  159.     ; claim the phase 2 workspace
  160.     LDR    R0,[R6,#Compress_Phase2Length]
  161.     BL    claim
  162.     STR    R0,[R6,#Compress_Phase2Ptr]
  163.     ; fall through into next_pass
  164.  
  165. next_pass
  166.     ; start scanning the source from the beginning again
  167.     MOV    R0,#1
  168.     MOV    R1,R8
  169.     MOV    R2,#1
  170.     SWI    XOS_Args
  171.     BVS    close_and_error
  172.     B    mainloop
  173.  
  174. claim_work
  175.     ; claim the phase 1 workspace
  176.     LDR    R0,[R6,#Compress_Phase1Length]
  177.     BL    claim
  178.     STR    R0,[R6,#Compress_Phase1Ptr]
  179.     B    mainloop
  180.  
  181. finished
  182.     ; close destination file
  183.     MOV    R0,#0
  184.     MOVS    R1,R7
  185.     SWINE    XOS_Find
  186.     MOV    R7,#0
  187.  
  188.     ; close source file
  189.     MOV    R0,#0
  190.     MOVS    R1,R8
  191.     SWINE    XOS_Find
  192.     MOV    R8,#0
  193.  
  194.     ; set output filetype
  195.     MOV    R0,#18
  196.     ADR    R1,outfile
  197.     LDR    R2,[R6,#Compress_SampleType]
  198.     SWI    OS_File
  199.  
  200.     ; exit
  201.     SWI    OS_Exit
  202.  
  203. ; --------------------------------------------------------------------------
  204.  
  205. dont_recognise_code
  206.     ADR    R0,dont_recognise_error
  207.     B    close_and_error
  208. dont_recognise_error
  209.     &    1
  210.     =    "Invalid reason code returned from Compress",0
  211.     ALIGN
  212.  
  213.  
  214. unknown_format
  215.     ADR    R0,unknown_format_error
  216.     B    close_and_error
  217. unknown_format_error
  218.     &    1
  219.     =    "Unrecognised file format",0
  220.     ALIGN
  221.  
  222. ; --------------------------------------------------------------------------
  223.  
  224. close_and_error
  225.     ; save error
  226.     STMFD    R13!,{R0}
  227.  
  228.     ; close destination file
  229.     MOV    R0,#0
  230.     MOVS    R1,R7
  231.     SWINE    XOS_Find
  232.     MOV    R7,#0
  233.  
  234.     ; close source file
  235.     MOV    R0,#0
  236.     MOVS    R1,R8
  237.     SWINE    XOS_Find
  238.     MOV    R8,#0
  239.  
  240.     ; generate error
  241.     LDMFD    R13!,{R0}
  242.     SWI    OS_GenerateError
  243.  
  244. ; --------------------------------------------------------------------------
  245.  
  246. infile    = "RAM:$.Infile",0
  247.     ALIGN
  248.  
  249. outfile = "RAM:$.Outfile",0
  250.     ALIGN
  251.  
  252. openinerror
  253.     &    1
  254.     =    "Unable to open input file",0
  255.     ALIGN
  256.  
  257. openouterror
  258.     &    1
  259.     =    "Unable to open output file",0
  260.     ALIGN
  261.  
  262. ; --------------------------------------------------------------------------
  263.  
  264. claim
  265.     ; r0 = size to claim
  266.     ; r12 = ptr to available workspace
  267.     ; r11 = ptr to end of available workspace
  268.     ; on exit, r0 = ptr to block claimed
  269.     ; an error occurs if not enough memory is available
  270.  
  271.     ADD    R12,R12,R0    ; update ptr
  272.     SUB    R0,R12,R0    ; ptr to new block
  273.     CMP    R12,R11        ; enough memory?
  274.     MOVLES    PC,R14        ; exit if so
  275.  
  276.     ; generate an error
  277.     ADR    R0,claim_error
  278.     B    close_and_error
  279.  
  280. claim_error
  281.     &    1
  282.     =    "Not enough memory available",0
  283.     ALIGN
  284.  
  285.  
  286.  
  287.     END
  288.