*****************************************************************************
*                                                                           *
* Ass-Subroutine to convert packed STAD-Pictures to 32k pictures (normal    *
* screenformat).                                                            *
*                                                                           *
* params:   (sp) LONG  return-adress                                        *
*          4(sp) LONG  adress of packed STAD-pictures                       *
*          8(sp) LONG  adress of destination (32k buffer)                   *
*                                                                           *
* returns:   d0        0 = did correct convert                              *
*                      1 = pictures is not in packed STAD-format :-(        *
*                                                                           *
* important: this is a special-version to use with MAXON-Pascal             *
*            the arguments will be removed, the return-value (word)         *
*            is at the top of the stack                                     *
*                                                                           *
* History:                                                                  *
* Version 1.0   01.06.1991  Wolfgang Ley                                    *
*                           - first version                                 *
* Version 1.1   03.06.1991  Wolfgang Ley                                    *
*                           - saving registers now                          *
* Version 1.2   05.06.1991  Wolfgang Ley                                    *
*                           - extra-counter in v_lese removed               *
* Version 1.3   06.07.1992  Wolfgang Ley                                    *
*                           - code cleaned up                               *
*                                                                           *
*****************************************************************************

                GLOBAL fromstad

                TEXT

fromstad:       movem.l D1-D5/A0-A1,-(SP) ;save registers

                movea.l 32(SP),A0       ;adress of packed STAD-picture
                movea.l 36(SP),A1       ;adress of destination (32k)

; ---------------------------------------------------------------------------
; STAD-Header:
;   Byte 1-4  string 'pM85' (horizontal packed)
;                    'pM86' (vertical packed)
;   Byte 5    ID-Byte
;   Byte 6    PACK-Byte
;   Byte 7    SPECIAL-Byte
;   ....      picture datas
; ---------------------------------------------------------------------------

                move.b  4(A0),D1        ;read ID-byte
                move.b  5(A0),D2        ;read PACK-byte
                move.b  6(A0),D3        ;read SPEC-byte
                clr.w   D4              ;clear dest.offset

                cmpi.b  #'p',(A0)+      ;first  byte = "p"
                bne.s   error
                cmpi.b  #'M',(A0)+      ;second byte = "M"
                bne.s   error
                cmpi.b  #'8',(A0)+      ;third  byte = "8"
                bne.s   error
                cmpi.b  #'5',(A0)       ;fourth byte = "5"
                beq.s   h_start         ;-> horizontal packed
                cmpi.b  #'6',(A0)       ;fourth byte = "6"
                beq.s   v_start         ;-> vertical packed

error:          move.l  #1,D0           ;error 1 = no packed STAD-picture
exit:           movem.l (SP)+,D1-D5/A0-A1 ;restore registers

                movea.l (SP)+,A0        ;return adress
                addq.l  #8,SP           ;skip arguments
                move.w  D0,(SP)         ;store return-value
                jmp     (A0)            ;that's it

; ---------------------------------------------------------------------------
; horizontal decompress routine
; ---------------------------------------------------------------------------

h_start:        addq.l  #4,A0           ;length of header
h_read:         move.b  (A0)+,D0        ;read next byte
                cmp.b   D1,D0           ;ID-Byte?
                beq.s   h_use_pack      ;yes -> use PACK-byte
                cmp.b   D3,D0           ;SPEC-Byte?
                beq.s   h_use_spec      ;yes -> use SPEC-byte
                move.b  D0,0(A1,D4.w)   ;use Byte "as is"
                addq.w  #1,D4           ;increment offset
h_cont:         cmpi.w  #32000,D4       ;everything read in?
                bne.s   h_read          ;no -> read next

ready:          clr.l   D0              ;return 0 = everything ok
                bra.s   exit            ;that's it...

; ---------------------------------------------------------------------------
; if you got an ID-byte: read next byte (n) and use PACK-Byte n+1 times
; ---------------------------------------------------------------------------

h_use_pack:     move.b  D2,D5           ;PACK-byte to write-byte
                bra.s   h_unpack        ;...and unpack

; ---------------------------------------------------------------------------
; if you got a SPEC-byte: read two more byte (d and n) and use the byte d
; n+1 times
; ---------------------------------------------------------------------------

h_use_spec:     move.b  (A0)+,D5        ;read next byte d

h_unpack:       clr.w   D0              ;clear...
                move.b  (A0)+,D0        ;read quantity

h_unpack_1:     move.b  D5,0(A1,D4.w)   ;write byte
                addq.w  #1,D4
                cmpi.w  #32000,D4       ;ready?
                dbeq    D0,h_unpack_1   ;next turn...
                bra.s   h_cont          ;read next byte from source

; ---------------------------------------------------------------------------
; verticale decompress routine
; ---------------------------------------------------------------------------

v_start:        addq.l  #4,A0           ;don't read header
v_read:         move.b  (A0)+,D0        ;read byte
                cmp.b   D1,D0           ;ID-Byte?
                beq.s   v_use_pack      ;yes -> use PACK-byte
                cmp.b   D3,D0           ;SPEC-Byte?
                beq.s   v_use_spec      ;yes -> use SPEC-byte
                move.b  D0,0(A1,D4.w)   ;use byte "as is"
                addi.l  #80,D4          ;increment offset
                cmpi.w  #32000,D4       ;column ready?
                blo.s   v_read          ;no, continue read
                subi.w  #31999,D4       ;correct offset to next column
                cmpi.w  #80,D4          ;everything read in?
                bne.s   v_read          ;no, continue read
                bra.s   ready           ;yes -> ready

; ---------------------------------------------------------------------------
; ...see h_use_pack
; ---------------------------------------------------------------------------

v_use_pack:     move.b  D2,D5           ;PACK-byte to write-byte
                bra.s   v_unpack        ;...and unpack

v_use_spec:     move.b  (A0)+,D5        ;read next byte d
v_unpack:       clr.w   D0              ;clear...
                move.b  (A0)+,D0        ;read quantity
v_unpack_1:     move.b  D5,0(A1,D4.w)   ;write byte
                addi.l  #80,D4          ;increment offset
                cmpi.w  #32000,D4       ;column complete?
                blo.s   v_unpack_2      ;no, continue read
                subi.w  #31999,D4       ;yes, correct offset to next column
                cmpi.w  #80,D4          ;everything read in?
                beq.s   ready           ;yes, ready
v_unpack_2:     dbra    D0,v_unpack_1   ;...next turn
                bra.s   v_read          ;read next byte from source


                END
