home *** CD-ROM | disk | FTP | other *** search
/ Falcon 030 Power 2 / F030_POWER2.iso / ST_STE / MAGS / ICTARI08.ARJ / ictari.08 / ASSEMBLY / LAYOUT.S next >
Text File  |  1994-02-15  |  14KB  |  470 lines

  1. *
  2. * Written with DevpacST 2, Medium rez, tabs set to 8.
  3. *
  4. * By, Mike Barnard, December 1st to 4th 1993. 
  5. *
  6. * This program is not about 'how to display a picture on-screen', although
  7. * that's what it does. It's about how I like to write and comment my
  8. * source code so a stranger can read and understand it. So many listings
  9. * I've seen have been continuous. Line after line of mnemonics, operands
  10. * and data. No breaks, separations or logical structure. Trying to find
  11. * out how the author was thinking takes hours of reading each line,
  12. * deciding what it does, commenting it myself, finding where the routines
  13. * start and end, inserting blank lines for clarity... what a time waste.
  14. *
  15. * I'm nowhere near good, I'm a learner. This is just MY idea of how code
  16. * CAN be commented. Except for one thing. A neighbour of mine once told
  17. * me about the 'Rule of Seven'. It basically means that when writing code,
  18. * no single chunk of code should be longer than 7 lines if you can help
  19. * it. It's not absolute! In programming, what's right is what works. But
  20. * it's true that the human brain finds it difficult to digest more than
  21. * that amount of information at a time. (If you can read a whole 
  22. * listing at a glance then lucky you, but I can't, and I don't know of
  23. * anyone else who can...).
  24. *
  25. * So anyway. This little program loads a Degas picture file from disk
  26. * and displays it to the standard screen memory. Then it looks for the
  27. * escape key to be pressed and returns to the desktop. Remember, It's not
  28. * WHAT it does, just how I've listed the source. Adding comments may
  29. * swell the source file, but the end program won't be hurt for it. I'm
  30. * just learning how to make this machine do what I want it to, and I TRY
  31. * to understand other peoples listings when they release them. Please try
  32. * to make them readable. For others, and for yourself. Thanks.
  33. *
  34. * Oh, you'll have to supply a degas low res picture yourself. I don't
  35. * want to waste too much disk space.
  36. *
  37. * Any comments, good, bad or indifferent...
  38. *
  39. * Mike Barnard
  40. * 52 Westbourne Avenue
  41. * Worthing
  42. * West Sussex
  43. * BN14 8DF
  44. *
  45. * (NO CALLING AT THE DOOR! Please.)
  46. *
  47. * I hope my ideas are useful.
  48. *************************************************************************
  49. *                                    *
  50. *                LAYOUT.S                *
  51. *                                    *
  52. *************************************************************************
  53.  
  54.         TEXT
  55.         
  56. start        jsr    init    goto the initialise routine
  57.         jsr    loadit    get the picture from the disk
  58. keyloop        jsr    getkey    if key pressed, get it's scancode
  59.         jsr    whatkey    find if it's <esc>
  60.         bra    keyloop    if not go back and look again...            
  61.  
  62. *************************************************************************
  63.  
  64. * IS THE SCANCODE THE <ESC> KEY?
  65.  
  66. whatkey
  67.  
  68.     move.w    scancode,d0    put current code into d0
  69.     cmpi.w    #1,d0        is it 1? (<ESCAPE> key)
  70.     beq    exit        yes, branch
  71.     
  72.     rts            no, go back
  73.     
  74. *************************************************************************
  75.  
  76. * RESTORE AND RETURN TO DESKTOP
  77.  
  78. exit
  79.  
  80. * restore screen resolution
  81.     
  82.     move.w    oldrez,-(sp)    screen rez required. (0=low, 1=med.)
  83.     move.l    #-1,-(sp)    negative number, no change to addresses
  84.     move.l    #-1,-(sp)    negative number, no change to addresses
  85.     move.w    #5,-(sp)    function number
  86.     trap    #14        XBIOS 14 - SETSCREEN
  87.     add.l    #12,sp        tidy
  88.  
  89. * restore palette
  90.  
  91.     move.l    #oldpal,-(sp)        palette to load
  92.     move.w    #6,-(sp)        function number
  93.     trap    #14            XBIOS 14 - SETPALETTE
  94.     addq.l    #6,sp             tidy
  95.     
  96. * exit to desktop
  97.  
  98.     move.w    #0,-(sp)    function number
  99.     trap    #1        byeeeeeeee.....
  100.  
  101. *************************************************************************
  102.  
  103. * HAS A KEY BEEN PRESSED?
  104.  
  105. getkey
  106.  
  107.     movem.l    d0-d7/a0-a6,-(sp)    Save registers
  108.     move.w    #0,scancode        Ensure null return unless key found
  109.     
  110. * has a key been pressed?
  111.  
  112.     move.w    #$0b,-(sp)    GEMDOS $0B - KEYBOARD BUFFER STATUS?
  113.     trap    #1        Result returned in d0.
  114.     addq.l    #2,sp        tidy
  115.  
  116.     tst.w    d0        d0.w=0, no keys pressed, -1 = yes
  117.     beq    .end        no keypresses in buffer, so return
  118.     
  119. * yes, found a keypress. Now get it's scancode
  120.  
  121.     move.w    #$08,-(sp)    GEMDOS 8 - GET A KEYPRESS
  122.     trap    #1
  123.     addq.l    #2,sp
  124.  
  125.     swap    d0        put scancode into low word
  126.     move.w    d0,scancode    save the scancode for later
  127.  
  128. .end    
  129.     movem.l    (sp)+,d0-d7/a0-a6    restore registers
  130.  
  131.     rts
  132.  
  133. *************************************************************************
  134.  
  135. * LOAD A DEGAS PICTURE FROM A DISK AND DISPLAY
  136.  
  137. loadit
  138.  
  139. * open the file, store the handle
  140.  
  141.     move.w    #0,-(sp)    file attribute (0=nornal, read\write)
  142.     move.l    #filename,-(sp)    address of file name string
  143.     move.w    #61,-(sp)    function number
  144.     trap    #1        GEMDOS 61 - OPEN A FILE
  145.     addq.l    #8,sp        tidy
  146.     
  147.     tst    d0        check for error
  148.     bmi    file_error    yes, branch
  149.  
  150.     move.w    d0,handle    store the file handle
  151.  
  152. * read the picture header to a buffer
  153.  
  154.     move.l    #headbuf,-(sp)    where to put the data
  155.     move.l    #34,-(sp)    how many bytes to read
  156.     move.w    handle,-(sp)    file handle
  157.     move.w    #63,-(sp)    function number
  158.     trap    #1        GEMDOS 63 - READ BYTES
  159.     add.l    #12,sp        tidy
  160.     
  161. * set the palette from the header
  162.  
  163.     move.l    #headbuf+2,-(sp)    palette starts 2 bytes into header
  164.     move.w    #6,-(sp)        function number
  165.     trap    #14            XBIOS 14 - SETPALETTE
  166.     addq.l    #6,sp             tidy
  167.  
  168. * read the picture data
  169.  
  170.     move.l    scrnp,-(sp)    where to put the data
  171.     move.l    #32000,-(sp)    how many bytes to read
  172.     move.w    handle,-(sp)    file handle
  173.     move.w    #63,-(sp)    function number
  174.     trap    #1        GEMDOS 63 - READ BYTES
  175.     add.l    #12,sp        tidy
  176.  
  177. * close the file
  178.  
  179.     move.w    handle,-(sp)    file handle
  180.     move.w    #62,-(sp)    function number
  181.     trap    #1        GEMDOS 62 - CLOSE A FILE
  182.     addq.l    #4,sp        tidy
  183.     
  184.     rts
  185.  
  186. *************************************************************************
  187.  
  188. * INITIALISE THE PROGRAM
  189.  
  190. init
  191.     
  192. * find current rez. Returned in d0
  193.  
  194.     move.w    #4,-(sp)    function number
  195.     trap    #14        XBIOS 4 - GETREZ
  196.     addq.l    #2,sp        tidy
  197.     
  198.     move.w    d0,oldrez    save it
  199.     
  200. * is it low? If not set it to low. 
  201.  
  202. * (If you are intending to trace this program from within MONST to watch
  203. * it at work, you may get screen corruption when you attempt to run a
  204. * trace through this bit, changing the screen resolution. It took me ages
  205. * to find out how to cure it but a good read of the manual (page 107 for
  206. * version 2 users) shows the reason, and how to cure it. You haven't got
  207. * the manual? I hope you're not one of the thieves who have helped to ruin
  208. * the ST market!).
  209.  
  210.     cmpi.w    #0,d0        is d0 = 0? (low rez?)
  211.     beq    getpal        Yes, branch
  212.     
  213.     move.w    #0,-(sp)    screen rez required. (0=low)
  214.     move.l    #-1,-(sp)    negative number, no change to addresses
  215.     move.l    #-1,-(sp)    negative number, no change to addresses
  216.     move.w    #5,-(sp)    function number
  217.     trap    #14        XBIOS 14 - SETSCREEN
  218.     add.l    #12,sp        tidy
  219.     
  220. * get current palette and store it
  221.  
  222. getpal    
  223.  
  224.     move.l    #oldpal,a3    address of palette save buffer into a3
  225.     move.w    #0,d3        counter for 16 colour registers
  226.     
  227. .loop    move.w    #-1,-(sp)    negative means read not write
  228.     move.w    d3,-(sp)    the colour register to read
  229.     move.w    #7,-(sp)    function number
  230.     trap    #14        XBIOS 7 - SETCOLOR
  231.     addq.l    #6,sp        tidy
  232.     
  233.     move.w    d0,(a3)+    put the colour data into the buffer.
  234.     addq.w    #1,d3        point d3 to the next colour register
  235.     cmpi.w    #16,d3        is d3 pointing to colour register 16 yet?
  236.     blt    .loop        No, branch
  237.     
  238. * get screen start address and store it
  239.  
  240.     move.w    #2,-(sp)    function number
  241.     trap    #14        XBIOS 2 - PHYSBASE
  242.     addq.l    #2,sp        tidy
  243.     
  244.     move.l    d0,scrnp    save the physical screen address
  245.     
  246.     rts
  247.     
  248. *************************************************************************
  249.  
  250. * DEAL WITH FILE ERRORS
  251.  
  252. file_error
  253.     
  254. * set the character colour to blue. (I found the error messages were
  255. * being printed in yellow, difficult to read, so this allows them to be
  256. * read).
  257.  
  258.     move.l    d0,errnumf    save the error code
  259.  
  260.     move.l    #setbl,-(sp)    address of string to set character colour
  261.     move.w    #9,-(sp)    function number
  262.     trap    #1        GEMDOS 9 - PRINT A STRING
  263.     addq.l    #6,sp        tidy
  264.  
  265.     move.l    errnumf,d0    return the error code
  266.  
  267. * is the error 'file not found'?
  268.  
  269.     cmpi.l    #-33,d0        is the error code -33?
  270.     bne    .1        no, branch
  271.     
  272. * if so, say so
  273.  
  274.     move.b    #9,cursx    select the x cursor position
  275.     move.b    #5,cursy    select the Y cursor position
  276.     jsr    setcurs        set the cursor
  277.  
  278.     move.l    #ermes01,-(sp)    address of message (file not found)
  279.     move.w    #9,-(sp)    function number
  280.     trap    #1        GEMDOS 9 - PRINT A STRING
  281.     addq.l    #6,sp        tidy
  282.     
  283.     bra    .2        goto next message
  284.  
  285. * else say general file error
  286.     
  287. .1    move.b    #8,cursx    select the x cursor position
  288.     move.b    #5,cursy    select the Y cursor position
  289.     jsr    setcurs        set the cursor
  290.  
  291.     move.l    #ermes02,-(sp)    address of message (file error)
  292.     move.w    #9,-(sp)    function number
  293.     trap    #1        GEMDOS 9 - PRINT A STRING
  294.     addq.l    #6,sp        tidy
  295.     
  296. * and print the error code number
  297.  
  298.     move.b    #8,cursx    select the x cursor position
  299.     move.b    #7,cursy    select the Y cursor position
  300.     jsr    setcurs        set the cursor
  301.  
  302.     move.l    #ermes04,-(sp)    address of message (error code is :)
  303.     move.w    #9,-(sp)    function number
  304.     trap    #1        GEMDOS 9 - PRINT A STRING
  305.     addq.l    #6,sp        tidy
  306.     
  307.     clr.l    d0        ensure d0 has no stray data
  308.     move.w    errnumf+2,d0    put the (word) error code into d0
  309.     move.l    #bidebuf,a0    load address of the binary/decimal buffer
  310.     jsr    bins2dec    convert signed bin. num to decimal string
  311.     
  312.     move.l    #bidebuf+1,-(sp)    address of ascii error code string
  313.     move.w    #9,-(sp)    function number
  314.     trap    #1        GEMDOS 9 - PRINT A STRING
  315.     addq.l    #6,sp        tidy
  316.  
  317. * say 'press escape'
  318.  
  319. .2    move.b    #8,cursx    select the x cursor position
  320.     move.b    #9,cursy    select the Y cursor position
  321.     jsr    setcurs        set the cursor
  322.  
  323.     move.l    #ermes03,-(sp)    address of message (press escape)
  324.     move.w    #9,-(sp)    function number
  325.     trap    #1        GEMDOS 9 - PRINT A STRING
  326.     addq.l    #6,sp        tidy
  327.     
  328. * go to exit routine
  329.  
  330.     bra    keyloop
  331.  
  332. *************************************************************************
  333.  
  334. * PRINT A SIGNED, WORD-SIZED, NUMBER TO THE SCREEN, IN DECIMAL.
  335.  
  336. * requires a 7 byte buffer
  337. * byte 1 - header, number of digits in the string
  338. * byte 2 to 7 - space for a 6 digit string. (A minus sign if needed, and
  339. *         up to five numeric digits).
  340.  
  341. * put the number to be printed into the low word of d0
  342. * put the address of the 7 byte buffer into a0
  343.  
  344. bins2dec
  345.  
  346. * set up the registers
  347.  
  348.     movem.l    a0-a1/d0-d4,-(sp)    save registers
  349.     
  350.     lea    1(a0),a1    point a1 to where the neg sign is to go
  351.     clr.l    d1        counter - number of decimal digits
  352.     clr.l    d2        flag - no leading nonzero digit found d2=0
  353.     
  354. * is the number negative?
  355.     
  356.     tst.w    d0        is d0 negative?
  357.     bpl    calcdg        no, branch
  358.  
  359. * if so, negate d0 & put a minus sign first on the string
  360.  
  361.     neg.w    d0        take d0 from 0. result in d0.
  362.     move.b    #"-",(a1)+    put the ascii for a minus sign in buffer
  363.     addq.b    #1,d1        inc number of digits counter
  364.     
  365. * now calculate the separate digits
  366.     
  367. calcdg    move.w    #10000,d3    d3 = the divisor
  368.     bsr    divs16        divide d0 by d3 and save ascii in buffer
  369.  
  370.     move.w    #1000,d3
  371.     bsr    divs16
  372.  
  373.     move.w    #100,d3
  374.     bsr    divs16    
  375.  
  376.     move.w    #10,d3
  377.     bsr    divs16    
  378.     
  379.     add.b    #'0',d0        convert the units digit to ascii
  380.     move.b    d0,(a1)+    always save the units digit
  381.     addq.b    #1,d1        inc number of digits
  382.     move.b    d1,(a0)        put the number of digits at the front of buffer
  383.  
  384.     movem.l    (sp)+,a0-a1/d0-d4    restore registers
  385.     
  386.     rts    
  387.  
  388. * divide a (long) number in d0 by a (word) number in d3 low word
  389.     
  390. divs16    divu    d3,d0        unsigned division
  391.     move.w    d0,d4        save the answer to d4
  392.     clr.w    d0        set low word of d0 to 0
  393.     swap    d0        put the remainder into low word of d0
  394.     
  395.     tst.b    d2        test the leading zero flag
  396.     bne    svdig        branch if the flag is not zero
  397.     
  398.     tst.b    d4        it is, so test this digit for zero
  399.     beq    enddiv        it is. No numbers left, so branch
  400.     addq.b    #1,d2        d4 not zero, so set the flag to say so
  401.  
  402. * save the number in d4 to the buffer as ascii
  403.  
  404. svdig    add.b    #'0',d4        make into ascii
  405.     addq.b    #1,d1        inc number of digits
  406.     move.b    d4,(a1)+    save ascii digit in buffer
  407.     
  408. enddiv    rts    
  409.     
  410. *************************************************************************
  411.  
  412. * SET THE CURSOR POSITION
  413.  
  414. * ALWAYS set BOTH 'cursx' & 'cursy' values before calling.  Note that if 
  415. * one of them is left from a previous call to this subroutine, it will have
  416. * #31 added to it AGAIN!
  417.  
  418. setcurs
  419.  
  420.     movem.l    d0-d7/a0-a6,-(sp)    save registers
  421.  
  422. * ascii the cursor references
  423.  
  424.     add.b    #31,cursx    adding #31 makes it an ascii code
  425.     add.b    #31,cursy    adding #31 makes it an ascii code
  426.  
  427. * print the cursor string, setting the screen cursor
  428.  
  429.     move.l    #curspos,-(sp)    address of string onto stack
  430.     move.w    #9,-(sp)    GEMDOS 9 - PRINT A STRING
  431.     trap    #1
  432.     addq.l    #6,sp
  433.     
  434.     movem.l    (sp)+,d0-d7/a0-a6    restore registers
  435.  
  436.     rts
  437.  
  438. *************************************************************************
  439.  
  440. * RESERVED MEMORY
  441.  
  442.         DATA
  443.  
  444. ermes01        dc.b    "Can't find the file...",0
  445. ermes02        dc.b    "File problem... oh dear!",0
  446. ermes03        dc.b    "Press <ESCAPE> to exit...",0
  447. ermes04        dc.b    "The error code is : ",0
  448. errnumf        dc.l    0,0        error code store and printing string
  449. filename    dc.b    "PIC.PI1",0
  450. setbl        dc.b    27,"b",8,0    set character print to colour blue
  451.  
  452. curspos        dc.b    27,"Y"        print this string to place the
  453. cursy        dc.b    32        cursor where you want it.
  454. cursx        dc.b    32
  455.         dc.b    0
  456.         
  457.         BSS
  458.  
  459.         even
  460. bidebuf        ds.b    8    buffer for printing signed numbers in dec
  461. handle        ds.w    1    file handle
  462. headbuf        ds.w    17    picture header
  463. oldrez        ds.w    1    original screen rez
  464. oldpal        ds.w    16    original colour palette
  465. scancode    ds.w    1    code from a keypress
  466. scrnp        ds.l    1    physical screen address
  467.  
  468. ****************************** T H E   E N D ******************************
  469.