Perihelion tutorial 2: Difference between revisions

From AtariForumWiki
Jump to navigation Jump to search
>Admin
No edit summary
 
>Zchris
m (Cleaned up some characters that displayed oddly)
 
(9 intermediate revisions by 4 users not shown)
Line 1: Line 1:
  <table border="0" width="70%">
'''The Atari ST M68000 tutorial part 2 of the workings of Devpac 3 and the realisation of some code'''
<tr>
      <td width="100%">
<p align="center"><span style="" lang="EN-GB"><font color="#ffffcc" face="Verdana" size="3"><b>The
Atari ST M68000 tutorial part 2 – of the workings of Devpac 3 and the
realisation of some code</b></font></span></p>
        <p>&nbsp;</p></td>


    </tr>
Hello again. It's only been some days since the last tutorial, but I'm bored right now and I don't have any computer game I feel like playing. This weekend went great, as it was GothCon (game convention here in Göteborg, Sweden) which meant games were afoot. I met Aldebaran for the first time IRL and he demonstrated the game Illuminati, which I plan to play later on this evening. But you don't really want to listen to that, do you?
</table>
  </center>
</div>
<div align="center">
  <center>
  <table border="0" width="70%">
    <tr>
      <td width="100%">
        <p class="MsoNormal" style="" align="justify"><span style="" lang="EN-GB"><font color="#ffffff">&nbsp;


        <font face="Verdana" size="1">
In the last tutorial, we went through some basic theory behind programming, and now we are going to put that theory into practice. We want to do something so awfully cool as to change the background colour to something else. First, we need to gather and learn how to use the tools. The tools in this case are only one; Devpac 3.00 by HiSoft. It can be acquired from Pompey Pirates CD #114, other places, or perhaps you can even buy it :)
        Hello again. It’s only been some days since the last tutorial, but
        I’m bored right now and I don’t have any computer game I feel like
        playing. This weekend went great, as it was GothCon (game convention
        here in Göteborg, Sweden) which meant games were afoot. I met Aldebaran
        for the first time IRL and he demonstrated the game Illuminati, which I
        plan to play later on this evening. But you don’t really want to
        listen to that, do you? <o:p>
        </o:p>
        </font>
        </o:p></font></span></p>
        <p class="MsoNormal" align="justify"><span style="" lang="EN-GB"><span style=""><font color="#ffffff"><font face="Verdana" size="1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        </font>
        </font></span><font color="#ffffff" face="Verdana" size="1">In the last tutorial, we went
        through some basic theory behind programming, and now we are going to
        put that theory into practice. We want to do something so awfully cool
        as to change the background colour to something else. First, we need to
        gather and learn how to use the tools. The tools in this case are only
        one; Devpac 3.00 by HiSoft. It can be acquired from Pompey Pirates CD #114,
        other places, or perhaps you can even buy it :)<o:p>


        </o:p>
Once we start up Devpac, we want to change some settings to make our lives easier. Go into options - control, under the setting format, select ST RAM, depending on your memory size, you may also wish to change the memory buffer. The only two interesting settings here are ST RAM and Atari Executable. If you choose Atari Executable, you will get a .prg file every time you assemble (also known as compile) your source code, so in order to test your source code, you will have to quit Devpac, and run the .prg file, then start Devpac again; enormously clumsy. Instead, we put the .prg file directly into the ST RAM (memory, RAM stands for Random Access Memory), from where it can be executed directly. So for now, set the setting to ST RAM. It's only when you are done coding something, that you will want to change this setting to Atari Executable in order to get a normal .prg file. Go into options - options, and uncheck the "Check absolutes for missing #. This eliminates lots of error reports than usually aren't error reports. In the options - environment you can set the environment variables, if you run from diskette, they should probably be something like PATH=a:\bin, INCDIR=a:\incdir. Go into options - resident and make sure both Assembler and Debugger are checked, this will also save you lots of time if you have the memory (since I run on 4 megs, I don't have to worry). That's about it I think, you can play around with settings on your own if you like, but these are essentials in my opinion.
        </font></span></p>
        <p class="MsoNormal" align="justify"><span style="" lang="EN-GB"><span style=""><font color="#ffffff"><font face="Verdana" size="1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        </font>
        </font></span><font color="#ffffff" face="Verdana" size="1">Once we start up Devpac, we want to
        change some settings to make our lives easier. Go into options –
        control, under the setting format, select ST RAM, depending on your
        memory size, you may also wish to change the memory buffer. The only two
        interesting settings here are ST RAM and Atari Executable. If you choose
        Atari Executable, you will get a .prg file every time you assemble (also
        known as compile) your source code, so in order to test your source
        code, you will have to quit Devpac, and run the .prg file, then start
        Devpac again; enormously clumsy. Instead, we put the .prg file directly
        into the ST RAM (memory, RAM stands for Random Access Memory), from
        where it can be executed directly. So for now, set the setting to ST
        RAM. It’s only when you are done coding something, that you will want
        to change this setting to Atari Executable in order to get a normal .prg
        file. Go into options – options, and uncheck the “Check absolutes
        for missing #”. This eliminates lots of error reports than usually
        aren’t error reports. In the options – environment you can set the
        environment variables, if you run from diskette, they should probably be
        something like PATH=a:\bin, INCDIR=a:\incdir. Go into options –
        resident and make sure both Assembler and Debugger are checked, this
        will also save you lots of time if you have the memory (since I run on 4
        megs, I don’t have to worry). That’s about it I think, you can play
        around with settings on your own if you like, but these are essentials
        in my opinion. <o:p>
        </o:p>
        </font></span></p>
        <p class="MsoNormal" align="justify"><span style="" lang="EN-GB"><span style=""><font color="#ffffff"><font face="Verdana" size="1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;


        </font>
OK, we have our environment (Devpac) correctly configured, now we want to do something. Remembering the past lesson, we soon realize that to change the background colour, we will have to know what controls the background colour. With this tutorial should be a file called MEMORY.TXT, it's a listing of the ST's memory, great for reference, in it we find the following lines:
        </font></span><font color="#ffffff" face="Verdana" size="1">OK, we have our environment (Devpac)
        correctly configured, now we want to do something. Remembering the past
        lesson, we soon realize that to change the background colour, we will
        have to know what controls the background colour. With this tutorial should be a file called MEMORY.TXT, it's a listing of the ST's memory, great for reference, in it we find the following lines:<o:p>
        </o:p>
        </font></span></p>
        <p class="MsoNormal" style="text-indent: 65.2pt;"><span style="" lang="EN-GB"><font color="#ffffff" face="Verdana" size="1">&nbsp;<o:p>
        </o:p>
        </font></span></p>
        <p class="MsoNormal" style="text-indent: 65.2pt;"><span style="" lang="EN-GB"><font color="#ffffcc" face="Verdana" size="1">$FF8240|word
        |Video palette register 0<o:p>


        </o:p>
*$FF8240|word |Video palette register 0
        </font></span></p>
..
        <p class="MsoNormal" style="text-indent: 65.2pt;"><span style="" lang="EN-GB"><font color="#ffffcc" face="Verdana" size="1">…<o:p>
*$FF825E|word |Video palette register 15
        </o:p>
        </font></span></p>
        <p class="MsoNormal" style="text-indent: 65.2pt;"><span style="" lang="EN-GB"><font color="#ffffcc" face="Verdana" size="1">$FF825E|word
        |Video palette register 15<o:p>
        </o:p>
        </font></span></p>


        <p class="MsoNormal" style="text-indent: 65.2pt;"><span style="" lang="EN-GB"><font color="#ffffff" face="Verdana" size="1">&nbsp;<o:p>
As you may know, the ST is capable of displaying 16 colours at once, with a palette of 512 colours. Having a palette means that you don't have to stay with 16 fixed colours, by changing the palette you can, say have 16 hues of red, and then 16 hues of blue on the next screen. Imagine an artist that can only have 16 colours on each of his paintings, however, he doesn't have to stick with the same 16 colours each time. OK, it seems that the first colour starts at $ff8240, this is indeed colour 0 and it's the background colour, so by merely changing the value here, we should change the background colour.
        </o:p>
        </font></span></p>
        <p class="MsoNormal" style="text-indent: 65.2pt;" align="justify"><span style="" lang="EN-GB"><font color="#ffffff" face="Verdana" size="1">As
        you may know, the ST is capable of displaying 16 colours at once, with a
        palette of 512 colours. Having a palette means that you don’t have to
        stay with 16 fixed colours, by changing the palette you can, say have 16
        hues of red, and then 16 hues of blue on the next screen. Imagine an
        artist that can only have 16 colours on each of his paintings, however,
        he doesn’t have to stick with the same 16 colours each time. OK, it
        seems that the first colour starts at $ff8240, this is indeed colour 0
        and it’s the background colour, so by merely changing the value here,
        we should change the background colour. <o:p>
        </o:p>
        </font></span></p>
        <p class="MsoNormal" style="text-indent: 65.2pt;" align="justify"><span style="" lang="EN-GB"><font color="#ffffff" face="Verdana" size="1">I
        talked about palettes just now, the ST is built on what you call RGB,
        Red Blue Green, colour. Every colour is made up of 8 levels of Red,
        Green and Blue, because the computer always counts form 0, the range is
        between 0 – 7. 8 * 8 * 8 = 512. So, the colour $700 would mean as red
        as you can get (maximum value of Red, zero value of Green and Blue).
        $770 would be yellow, $777 would be white and $000 would be black.
        Equipped with this knowledge we enter this single line as our source
        code<o:p>
        </o:p>


        </font></span></p>
I talked about palettes just now, the ST is built on what you call RGB, Red Blue Green, colour. Every colour is made up of 8 levels of Red, Green and Blue, because the computer always counts form 0, the range is between 0 - 7. 8 * 8 * 8 = 512. So, the colour $700 would mean as red as you can get (maximum value of Red, zero value of Green and Blue). $770 would be yellow, $777 would be white and $000 would be black. Equipped with this knowledge we enter this single line as our source code
 
      <table border="0" cellspacing="5" width="100%">
        <tbody><tr>
          <td width="25%">
          </td>
          <td width="25%">
            <p align="left"><span style="" lang="EN-GB"><font color="#ccffff" face="Verdana" size="1">move.w</font></span></p></td>
  <td width="31%">


    <p align="left"><span style="" lang="EN-GB"><font color="#ffffcc" face="Verdana" size="1">#$700,$ffff8240</font></span></p></td>
*move.w #$700,$ffff8240       ; red background color
          <td width="44%">
            <p align="left"><span style="" lang="EN-GB"><font color="#ccffcc" face="Verdana" size="1">red
            background color<o:p>
            &nbsp;</o:p></font></span></p></td>
        </tr>
      </tbody></table>
  <center>
        <p class="MsoNormal" style="margin-left: 65.2pt; text-indent: 65.2pt;"><span style="" lang="EN-GB"><font color="#ffffff" face="Verdana" size="1"><span style="">&nbsp;&nbsp;</span><span style="">&nbsp; </span>&nbsp;<o:p>


        </o:p>
Move is the command used for moving values around other values, in this case, we move the value #$700 into memory position $ffff8240. The # indicates that what comes after is an absolute value, and the $ means that the value is hexadecimal, instead of decimal (more on this later, accept for now). The .w after the move instruction means that the move instruction should move a word, indicating the size of the thing you want moved (more on this later also, accept for now). So, the line above means in clear (?) English; move a value of word size, an absolute value expressed as a hexadecimal value 700 into the memory address $ffff8240. This should be enough to change the background colour to red.
        </font></span></p>
  </center>
        <p class="MsoNormal" style="text-indent: 65.2pt;" align="justify"><span style="" lang="EN-GB"><font color="#ffffff" face="Verdana" size="1">Move
        is the command used for moving values around other values, in this case,
        we move the value #$700 into memory position $ffff8240. The # indicates
        that what comes after is an absolute value, and the $ means that the
        value is hexadecimal, instead of decimal (more on this later, accept for
        now). The .w after the move instruction means that the move instruction
        should move a word, indicating the size of the thing you want moved
        (more on this later also, accept for now). So, the line above means in
        clear (?) English; move a value of word size, an absolute value
        expressed as a hexadecimal value 700 into the memory address $ffff8240.
        This should be enough to change the background colour to red. <o:p>
        </o:p>
        </font></span></p>
        <p class="MsoNormal" style="text-indent: 65.2pt;" align="justify"><span style="" lang="EN-GB"><font color="#ffffff" face="Verdana" size="1">Now
        we want to assemble the source code, and get our executable. Short
        command for this in Devpac is alt + a. Now, a window will pop up,
        displaying some statistics, what we want to search for is especially the
        line “0 errors found”. If there are errors found, the pointer will
        automatically move to the error so that you can correct it. By pressing
        alt + number, you can cycle through the available windows. So, if you do
        get an error, but you don’t understand it, press for example alt + 2,
        which will take you to the second window, where the error report
        probably is, try to understand something, then hit alt + 1 to go back
        and edit your source code. ctrl + w will close a window.<span style="">&nbsp;
        </span>Okidok, the source code has compiled successfully, now we need to
        run it; hit alt + x (eXecute). Now you will be asked to pass parameters
        to the program, don’t, just hit enter. Ohh, the expectation, will the
        background change to red?<o:p>


        </o:p>
Now we want to assemble the source code, and get our executable. Short command for this in Devpac is alt + a. Now, a window will pop up, displaying some statistics, what we want to search for is especially the line "0 errors found". If there are errors found, the pointer will automatically move to the error so that you can correct it. By pressing alt + number, you can cycle through the available windows. So, if you do get an error, but you don't understand it, press for example alt + 2, which will take you to the second window, where the error report probably is, try to understand something, then hit alt + 1 to go back and edit your source code. ctrl + w will close a window. Okidok, the source code has compiled successfully, now we need to run it; hit alt + x (eXecute). Now you will be asked to pass parameters to the program, don't, just hit enter. Ohh, the expectation, will the background change to red?
        </font></span></p>
        <p class="MsoNormal" style="text-indent: 65.2pt;" align="justify"><span style="" lang="EN-GB"><font color="#ffffff" face="Verdana" size="1">NO!
        We get two fucking bombs for all our effort. What the fuck? Is there
        anything wrong with the source code, no, it seems not. Is there anything
        wrong with the address? Double-checking the address value, no, $ffff8240
        means colour 0, which is the background colour. Well, the ST can operate
        in two modes, user and super mode. In the user mode, we aren’t allowed
        to access certain things in memory, for example the palette, the result
        if we try to do this is two bombs. So we need to go into super mode.
        Referring to a list of the so called trap functions of GEMDOS, ideally
        the ST Internals, we find out how to enter super mode, the code looks
        like this:<o:p>
        </o:p>
        </font></span></p>
  <center>
        <p class="MsoNormal" style="margin-left: 65.2pt; text-indent: 65.2pt;"><span style="" lang="EN-GB"><font color="#ffffff" face="Verdana" size="1">&nbsp;<o:p>
        </o:p>


        </font></span></p>
NO! We get two bombs for all our effort. What the f***? Is there anything wrong with the source code, no, it seems not. Is there anything wrong with the address? Double-checking the address value, no, $ffff8240 means colour 0, which is the background colour.  
  <div align="center">
    <table border="0" cellspacing="5" width="100%">
      <tbody><tr>
        <td align="left" width="24%"></td>
        <td align="left" width="24%"><span style="" lang="EN-GB"><font color="#ccffff" face="Verdana" size="1">clr.l</font></span></td>
        <td align="left" width="30%"><span style="" lang="EN-GB"><font color="#ffffcc" face="Verdana" size="1">-(a7)</font></span></td>
        <td width="46%"><span style="" lang="EN-GB"><font color="#ccffcc" face="Verdana" size="1">clear stack<o:p>


          &nbsp;</o:p></font></span></td>
Well, the ST can operate in two modes, user and super mode. In the user mode, we aren't allowed to access certain things in memory, for example the palette, the result if we try to do this is two bombs. So we need to go into super mode. Referring to a list of the so called trap functions of GEMDOS, ideally the ST Internals, we find out how to enter super mode, the code looks like this:
      </tr>
      <tr>
        <td align="left" width="24%"></td>
        <td align="left" width="24%"><span style="" lang="EN-GB"><font color="#ccffff" face="Verdana" size="1">move.w</font></span></td>
        <td align="left" width="30%"><span style="" lang="EN-GB"><font color="#ffffcc" face="Verdana" size="1">#32,-(a7)</font></span></td>
        <td width="46%"><span style="" lang="EN-GB"><font color="#ccffcc" face="Verdana" size="1">prepare
        for super mode</font></span></td>


      </tr>
* clr.l -(a7) clear stack 
      <tr>
* move.w #32,-(a7) prepare for super mode
        <td align="left" width="24%"></td>
* trap #1 call gemdos 
        <td align="left" width="24%"><span style="" lang="EN-GB"><font color="#ccffff" face="Verdana" size="1">trap</font></span></td>
* addq.l #6,a7 clear up stack 
        <td align="left" width="30%"><span style="" lang="EN-GB"><font color="#ffffcc" face="Verdana" size="1"><span style="">&nbsp;
* move.l d0,old_stack backup old stack pointer 
          </span>#1</font></span></td>
        <td width="46%"><span style="" lang="EN-GB"><font color="#ccffcc" face="Verdana" size="1">call
        gemdos<o:p>


          &nbsp;</o:p></font></span></td>
''(ok, so the code looks somewhat different than in ST Internals, a good lesson for you that you can write differently, but still achieve the same)''
      </tr>
      <tr>
        <td align="left" width="24%"></td>
        <td align="left" width="24%"><span style="" lang="EN-GB"><font color="#ccffff" face="Verdana" size="1">addq.l<span style="">&nbsp; </span></font></span></td>
        <td align="left" width="30%"><span style="" lang="EN-GB"><font color="#ffffcc" face="Verdana" size="1">#6,a7</font></span></td>
        <td width="46%"><span style="" lang="EN-GB"><font color="#ccffcc" face="Verdana" size="1">clear up stack<o:p>


          &nbsp;</o:p></font></span></td>
Perhaps I shouldn't go to deeply into this, it will come in the next tutorial where I'll take up the different registers and talk about traps and so on. For now, you'll just have to accept it, but for you curious types, here's a short one.
      </tr>
      <tr>
        <td align="left" width="24%"></td>
        <td align="left" width="24%"><span style="" lang="EN-GB"><font color="#ccffff" face="Verdana" size="1">move.l</font></span></td>
        <td align="left" width="30%"><span style="" lang="EN-GB"><font color="#ffffcc" face="Verdana" size="1">d0,old_stack</font></span></td>
        <td width="46%"><span style="" lang="EN-GB"><font color="#ccffcc" face="Verdana" size="1">backup
        old stack pointer<o:p>


          &nbsp;</o:p></font></span></td>
The GEMDOS have several special functions, which are accessed by the trap #1 command (calling trap #13 calls BIOS functions). The controlling value is put on the stack, which is address register a7 (you can also type sp, short for stack pointer, instead of a7). The move.l d0, old_stack is for backing up the old user stack, that gets replaced when we go into super mode.
      </tr>
    </tbody></table>
  </div>
        <p class="MsoNormal" style="margin-left: 65.2pt; text-indent: 65.2pt;"><span style="" lang="EN-GB"><font color="#ffffff" face="Verdana" size="1">&nbsp;<o:p>
        </o:p>
        </font></span></p>
  </center>
        <p class="MsoNormal" style="text-indent: 65.2pt;" align="justify"><span style="" lang="EN-GB"><font color="#ffffff" face="Verdana" size="1"><span style="">&nbsp;</span>(ok,
        so the code looks somewhat different than in ST Internals, a good lesson
        for you that you can write differently, but still achieve the same)<o:p>


        </o:p>
This code obviously goes at the top of our source code, the first thing we want is to go into super mode, then we put red colour in palette register 0, lastly, we want to go back into user mode and also add another two lines of accept now, understand later code, which will make a clean exit of the program. The total code looks like this:
        </font></span></p>
    
        <p class="MsoNormal" style="text-indent: 65.2pt;" align="justify"><span style="" lang="EN-GB"><font color="#ffffff" face="Verdana" size="1">Perhaps
* clr.l -(a7)         clear stack
        I shouldn’t go to deeply into this, it will come in the next tutorial
* move.w #32,-(a7) prepare for super mode
        where I’ll take up the different registers and talk about traps and so
* trap #1         call gemdos
        on. For now, you’ll just have to accept it, but for you curious types,
* addq.l #6,a7           clear up stack
        here’s a short one. The GEMDOS have several special functions, which
* move.l d0,old_stack   backup old stack pointer
        are accessed by the trap #1 command (calling trap #13 calls BIOS
        functions). The controlling value is put on the stack, which is address
        register a7 (you can also type sp, short for stack pointer, instead of
        a7). The move.l d0, old_stack is for backing up the old user stack, that
        gets replaced when we go into super mode. <o:p>
        </o:p>
        </font></span></p>
        <p class="MsoNormal" style="text-indent: 65.2pt;" align="justify"><span style="" lang="EN-GB"><font color="#ffffff" face="Verdana" size="1">This
        code obviously goes at the top of our source code, the first thing we
        want is to go into super mode, then we put red colour in palette
        register 0, lastly, we want to go back into user mode and also add
        another two lines of accept now, understand later code, which will make
        a clean exit of the program. The total code looks like this:<o:p>
        </o:p>
        </font></span></p>
 
   <center>
        <p class="MsoNormal" style="margin-left: 65.2pt; text-indent: 65.2pt;"><span style="" lang="EN-GB"><font color="#ffffff" face="Verdana" size="1">&nbsp;<o:p>
        &nbsp;
        </o:p></font></span></p>
  <table border="0" cellspacing="5" width="100%">
    <tbody><tr>
      <td align="left" width="24%"></td>
      <td align="left" width="24%"><span style="" lang="EN-GB"><font color="#ccffff" face="Verdana" size="1">clr.l</font></span></td>
 
      <td align="left" width="36%"><span style="" lang="EN-GB"><font color="#ffffcc" face="Verdana" size="1">-(a7)</font></span></td>
      <td width="40%"><span style="" lang="EN-GB"><font color="#ccffcc" face="Verdana" size="1">clear stack</font></span></td>
    </tr>
    <tr>
      <td align="left" width="24%"></td>
      <td align="left" width="24%"><span style="" lang="EN-GB"><font color="#ccffff" face="Verdana" size="1">move.w</font></span></td>
      <td align="left" width="36%"><span style="" lang="EN-GB"><font color="#ffffcc" face="Verdana" size="1">#32,-(a7)</font></span></td>
 
      <td width="40%"><span style="" lang="EN-GB"><font color="#ccffcc" face="Verdana" size="1">prepare
        for super mode</font></span></td>
    </tr>
    <tr>
      <td align="left" width="24%"></td>
      <td align="left" width="24%"><span style="" lang="EN-GB"><font color="#ccffff" face="Verdana" size="1">trap<span style="">&nbsp;
        </span></font></span></td>
      <td align="left" width="36%"><span style="" lang="EN-GB"><font color="#ffffcc" face="Verdana" size="1">#1</font></span></td>
 
      <td width="40%"><span style="" lang="EN-GB"><font color="#ccffcc" face="Verdana" size="1">call
        gemdos</font></span></td>
    </tr>
    <tr>
      <td align="left" width="24%"></td>
      <td align="left" width="24%"><span style="" lang="EN-GB"><font color="#ccffff" face="Verdana" size="1">addq.l<span style="">&nbsp; </span></font></span>
        <p>&nbsp;</p></td>
      <td align="left" width="36%"><span style="" lang="EN-GB"><font color="#ffffcc" face="Verdana" size="1">#6,a7</font></span>
 
        <p><font face="Verdana" size="1">&nbsp;</font></p></td>
      <td width="40%"><span style="" lang="EN-GB"><font color="#ccffcc" face="Verdana" size="1">clear up stack</font></span>
        <p><font face="Verdana" size="1">&nbsp;</font></p></td>
    </tr>
    <tr>
      <td align="left" width="24%"></td>
      <td align="left" width="24%"><span style="" lang="EN-GB"><font color="#ccffff" face="Verdana" size="1">move.l</font></span>
        <p><font face="Verdana" size="1">&nbsp;</font></p></td>
 
      <td align="left" width="36%"><span style="" lang="EN-GB"><font color="#ffffcc" face="Verdana" size="1">d0,old_stack</font></span>
        <p><font face="Verdana" size="1">&nbsp;</font></p></td>
      <td width="40%"><span style="" lang="EN-GB"><font color="#ccffcc" face="Verdana" size="1">backup
        old stack pointer</font></span>
        <p><span style="" lang="EN-GB"><font color="#ccffcc" face="Verdana" size="1"><o:p>
        &nbsp;</o:p></font></span></p></td>
    </tr>
    <tr>
      <td align="left" width="24%"></td>
 
      <td align="left" width="24%"><span style="" lang="EN-GB"><font color="#ccffff" face="Verdana" size="1">move.w</font></span></td>
      <td align="left" width="36%"><span style="" lang="EN-GB"><font color="#ffffcc" face="Verdana" size="1">#$700,$ffff8240</font></span></td>
      <td width="40%"><span style="" lang="EN-GB"><font color="#ccffcc" face="Verdana" size="1">red
        background color<o:p>
        &nbsp;</o:p></font></span></td>
    </tr>
    <tr>
      <td align="left" width="24%"></td>
 
      <td align="left" width="24%"><span style="" lang="EN-GB"><font color="#ccffff" face="Verdana" size="1">move.l</font></span></td>
      <td align="left" width="36%"><span style="" lang="EN-GB"><font color="#ffffcc" face="Verdana" size="1">old_stack,-(a7)</font></span></td>
      <td width="40%"><span style="" lang="EN-GB"><font color="#ccffcc" face="Verdana" size="1">restore
        old stack pointer<o:p>
        &nbsp;</o:p></font></span></td>
    </tr>
    <tr>
      <td align="left" width="24%"></td>
 
      <td align="left" width="24%"><span style="" lang="EN-GB"><font color="#ccffff" face="Verdana" size="1">move.w</font></span></td>
      <td align="left" width="36%"><span style="" lang="EN-GB"><font color="#ffffcc" face="Verdana" size="1">#32,-(a7)</font></span></td>
      <td width="40%"><span style="" lang="EN-GB"><font color="#ccffcc" face="Verdana" size="1">back to
        user mode<o:p>
        &nbsp;</o:p></font></span></td>
    </tr>
    <tr>
      <td align="left" width="24%"></td>
 
      <td align="left" width="24%"><span style="" lang="EN-GB"><font color="#ccffff" face="Verdana" size="1">trap<span style="">&nbsp;
        </span></font></span></td>
      <td align="left" width="36%"><span style="" lang="EN-GB"><font color="#ffffcc" face="Verdana" size="1">#1</font></span></td>
      <td width="40%"><span style="" lang="EN-GB"><font color="#ccffcc" face="Verdana" size="1">call
        gemdos</font></span></td>
    </tr>
    <tr>
      <td align="left" width="24%"></td>
 
      <td align="left" width="24%"><span style="" lang="EN-GB"><font color="#ccffff" face="Verdana" size="1">addq.l</font></span>
        <p><font face="Verdana" size="1">&nbsp;</font></p></td>
      <td align="left" width="36%"><span style="" lang="EN-GB"><font color="#ffffcc" face="Verdana" size="1">#6,a7<span style="">&nbsp; </span></font></span>
        <p>&nbsp;</p></td>
      <td width="40%"><span style="" lang="EN-GB"><font color="#ccffcc" face="Verdana" size="1">clear stack</font></span>
        <p><font face="Verdana" size="1">&nbsp;</font></p></td>
    </tr>
 
    <tr>
      <td align="left" width="24%"></td>
      <td align="left" width="24%"><span style="" lang="EN-GB"><font color="#ccffff" face="Verdana" size="1">clr.l</font></span></td>
      <td align="left" width="36%"><span style="" lang="EN-GB"><font color="#ffffcc" face="Verdana" size="1">-(a7)<span style="">&nbsp;
        </span></font></span></td>
      <td width="40%"><span style="" lang="EN-GB"><font color="#ccffcc" face="Verdana" size="1">clean exit<o:p>
        &nbsp;</o:p></font></span></td>
 
    </tr>
    <tr>
      <td align="left" width="24%"></td>
      <td align="left" width="24%"><span style="" lang="EN-GB"><font color="#ccffff" face="Verdana" size="1">trap</font></span></td>
      <td align="left" width="36%"><span style="" lang="EN-GB"><font color="#ffffcc" face="Verdana" size="1">#1<span style="">&nbsp;&nbsp;
        </span></font></span></td>
      <td width="40%"><span style="" lang="EN-GB"><font color="#ccffcc" face="Verdana" size="1">call gemdos<o:p>


        &nbsp;</o:p></font></span></td>
* move.w #$700,$ffff8240 red background color
    </tr>
* move.l old_stack,-(a7) restore old stack pointer 
    <tr>
* move.w #32,-(a7) back to user mode 
      <td align="left" width="24%"><span style="" lang="EN-GB"><font color="#ffffff" face="Verdana" size="1">old_stack</font></span></td>
* trap  #1         call gemdos
      <td align="left" width="24%"><span style="" lang="EN-GB"><font color="#ccffff" face="Verdana" size="1">dc.l</font></span></td>
* addq.l  #6,a7          clear stack
      <td align="left" width="36%"><span style="" lang="EN-GB"><font color="#ffffcc" face="Verdana" size="1">0</font></span></td>
      <td width="40%"><span style="" lang="EN-GB"><font color="#ccffcc" face="Verdana" size="1"><o:p>


        &nbsp;</o:p></font></span></td>
* clr.l -(a7)  clean exit 
    </tr>
* trap #1  call gemdos 
  </tbody></table>
*
        <p class="MsoNormal" style="margin-left: 65.2pt;"><span style="" lang="EN-GB"><font face="Verdana" size="1"><font color="#ffffff"><span style="">&nbsp;
*old_stack dc.l 0
        &nbsp;
        </span>
        </font><span style=""><font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        </font></span><font color="#ffffff"><o:p>
        </o:p>


        </font></font></span></p>
Running this program will successfully change the background colour to red, and then make a nice and clean exit, restoring the Atari to user mode once again. Problem is, we now have a red background. Not to good you might think. This can be easily remedied however, we have made a program that changes background colour, let's use it! Change the value #$700 to #$777 and run the program again. The background colour will now be white as snow, like we're used to.
  </center>
        <p class="MsoNormal" align="justify"><span style="" lang="EN-GB"><font color="#ffffff" face="Verdana" size="1"><span style="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        </span>Running this program will successfully change the background
        colour to red, and then make a nice and clean exit, restoring the Atari
        to user mode once again. Problem is, we now have a red background. Not
        to good you might think. This can be easily remedied however, we have
        made a program that changes background colour, let’s use it! Change
        the value #$700 to #$777 and run the program again. The background
        colour will now be white as snow, like we’re used to. <o:p>
        </o:p>
        </font></span></p>
        <p class="MsoNormal" align="justify"><span style="" lang="EN-GB"><span style=""><font color="#ffffff"><font face="Verdana" size="1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        </font>


        </font></span><font color="#ffffff" face="Verdana" size="1">In the next tutorial, I plan to
In the next tutorial, I plan to cover hexadecimal, binary and decimal numbers, program flow and talk some about traps and registers.
        cover hexadecimal, binary and decimal numbers, program flow and talk
        some about traps and registers. <o:p>
        </o:p>
        </font></span></p>
        <p class="MsoNormal" align="justify">&nbsp;</p>
  <center>
  </center>
      </td>
    </tr>


    <tr>
      <td width="100%">
Warrior Munk of poSTmortem, 2002-04-02
        <span style="" lang="EN-GB"><font color="#ffffff" face="Verdana" size="1">Warrior Munk of
''"No immovable roots, no stirring of dust, only the tao; the way of nature. This is called tai chi."'' - Tai Chi Master 
        poSTmortem, 2002-04-02<o:p>
        &nbsp;
        </o:p></font></span></td>
    </tr>
    <tr>
      <td width="100%"><table align="center" bgcolor="#cc0000" cellpadding="0" cellspacing="1" width="100%">


<tbody><tr>
Last edited 2002-06-14
<td>
<br/><br/>
proceed to [[Perihelion tutorial 3]]
<table cellpadding="0" cellspacing="0" width="100%">
<tbody><tr>
<td bgcolor="#000000">
        <font color="#ffffff" face="Verdana" size="1"><i>“No immovable roots, no stirring of dust, only the tao; the way of nature. This is called tai chi.” -&nbsp;</i></font>
        <p><font color="#ffffff" face="Verdana" size="1"><i> Tai Chi Master &nbsp;</i></font></p></td></tr>


</tbody></table>
[[Category: Perihelions Atari ST M68000 tutorial]]
</td>
</tr>
</tbody></table>

Latest revision as of 06:48, 8 October 2012

The Atari ST M68000 tutorial part 2 of the workings of Devpac 3 and the realisation of some code

Hello again. It's only been some days since the last tutorial, but I'm bored right now and I don't have any computer game I feel like playing. This weekend went great, as it was GothCon (game convention here in Göteborg, Sweden) which meant games were afoot. I met Aldebaran for the first time IRL and he demonstrated the game Illuminati, which I plan to play later on this evening. But you don't really want to listen to that, do you?

In the last tutorial, we went through some basic theory behind programming, and now we are going to put that theory into practice. We want to do something so awfully cool as to change the background colour to something else. First, we need to gather and learn how to use the tools. The tools in this case are only one; Devpac 3.00 by HiSoft. It can be acquired from Pompey Pirates CD #114, other places, or perhaps you can even buy it :)

Once we start up Devpac, we want to change some settings to make our lives easier. Go into options - control, under the setting format, select ST RAM, depending on your memory size, you may also wish to change the memory buffer. The only two interesting settings here are ST RAM and Atari Executable. If you choose Atari Executable, you will get a .prg file every time you assemble (also known as compile) your source code, so in order to test your source code, you will have to quit Devpac, and run the .prg file, then start Devpac again; enormously clumsy. Instead, we put the .prg file directly into the ST RAM (memory, RAM stands for Random Access Memory), from where it can be executed directly. So for now, set the setting to ST RAM. It's only when you are done coding something, that you will want to change this setting to Atari Executable in order to get a normal .prg file. Go into options - options, and uncheck the "Check absolutes for missing #". This eliminates lots of error reports than usually aren't error reports. In the options - environment you can set the environment variables, if you run from diskette, they should probably be something like PATH=a:\bin, INCDIR=a:\incdir. Go into options - resident and make sure both Assembler and Debugger are checked, this will also save you lots of time if you have the memory (since I run on 4 megs, I don't have to worry). That's about it I think, you can play around with settings on your own if you like, but these are essentials in my opinion.

OK, we have our environment (Devpac) correctly configured, now we want to do something. Remembering the past lesson, we soon realize that to change the background colour, we will have to know what controls the background colour. With this tutorial should be a file called MEMORY.TXT, it's a listing of the ST's memory, great for reference, in it we find the following lines:

  • $FF8240|word |Video palette register 0

..

  • $FF825E|word |Video palette register 15

As you may know, the ST is capable of displaying 16 colours at once, with a palette of 512 colours. Having a palette means that you don't have to stay with 16 fixed colours, by changing the palette you can, say have 16 hues of red, and then 16 hues of blue on the next screen. Imagine an artist that can only have 16 colours on each of his paintings, however, he doesn't have to stick with the same 16 colours each time. OK, it seems that the first colour starts at $ff8240, this is indeed colour 0 and it's the background colour, so by merely changing the value here, we should change the background colour.

I talked about palettes just now, the ST is built on what you call RGB, Red Blue Green, colour. Every colour is made up of 8 levels of Red, Green and Blue, because the computer always counts form 0, the range is between 0 - 7. 8 * 8 * 8 = 512. So, the colour $700 would mean as red as you can get (maximum value of Red, zero value of Green and Blue). $770 would be yellow, $777 would be white and $000 would be black. Equipped with this knowledge we enter this single line as our source code

  • move.w #$700,$ffff8240  ; red background color

Move is the command used for moving values around other values, in this case, we move the value #$700 into memory position $ffff8240. The # indicates that what comes after is an absolute value, and the $ means that the value is hexadecimal, instead of decimal (more on this later, accept for now). The .w after the move instruction means that the move instruction should move a word, indicating the size of the thing you want moved (more on this later also, accept for now). So, the line above means in clear (?) English; move a value of word size, an absolute value expressed as a hexadecimal value 700 into the memory address $ffff8240. This should be enough to change the background colour to red.

Now we want to assemble the source code, and get our executable. Short command for this in Devpac is alt + a. Now, a window will pop up, displaying some statistics, what we want to search for is especially the line "0 errors found". If there are errors found, the pointer will automatically move to the error so that you can correct it. By pressing alt + number, you can cycle through the available windows. So, if you do get an error, but you don't understand it, press for example alt + 2, which will take you to the second window, where the error report probably is, try to understand something, then hit alt + 1 to go back and edit your source code. ctrl + w will close a window. Okidok, the source code has compiled successfully, now we need to run it; hit alt + x (eXecute). Now you will be asked to pass parameters to the program, don't, just hit enter. Ohh, the expectation, will the background change to red?

NO! We get two bombs for all our effort. What the f***? Is there anything wrong with the source code, no, it seems not. Is there anything wrong with the address? Double-checking the address value, no, $ffff8240 means colour 0, which is the background colour.

Well, the ST can operate in two modes, user and super mode. In the user mode, we aren't allowed to access certain things in memory, for example the palette, the result if we try to do this is two bombs. So we need to go into super mode. Referring to a list of the so called trap functions of GEMDOS, ideally the ST Internals, we find out how to enter super mode, the code looks like this:

  • clr.l -(a7) clear stack
  • move.w #32,-(a7) prepare for super mode
  • trap #1 call gemdos
  • addq.l #6,a7 clear up stack
  • move.l d0,old_stack backup old stack pointer

(ok, so the code looks somewhat different than in ST Internals, a good lesson for you that you can write differently, but still achieve the same)

Perhaps I shouldn't go to deeply into this, it will come in the next tutorial where I'll take up the different registers and talk about traps and so on. For now, you'll just have to accept it, but for you curious types, here's a short one.

The GEMDOS have several special functions, which are accessed by the trap #1 command (calling trap #13 calls BIOS functions). The controlling value is put on the stack, which is address register a7 (you can also type sp, short for stack pointer, instead of a7). The move.l d0, old_stack is for backing up the old user stack, that gets replaced when we go into super mode.

This code obviously goes at the top of our source code, the first thing we want is to go into super mode, then we put red colour in palette register 0, lastly, we want to go back into user mode and also add another two lines of accept now, understand later code, which will make a clean exit of the program. The total code looks like this:

  • clr.l -(a7) clear stack
  • move.w #32,-(a7) prepare for super mode
  • trap #1 call gemdos
  • addq.l #6,a7 clear up stack
  • move.l d0,old_stack backup old stack pointer
  • move.w #$700,$ffff8240 red background color
  • move.l old_stack,-(a7) restore old stack pointer
  • move.w #32,-(a7) back to user mode
  • trap #1 call gemdos
  • addq.l #6,a7 clear stack
  • clr.l -(a7) clean exit
  • trap #1 call gemdos
  • old_stack dc.l 0

Running this program will successfully change the background colour to red, and then make a nice and clean exit, restoring the Atari to user mode once again. Problem is, we now have a red background. Not to good you might think. This can be easily remedied however, we have made a program that changes background colour, let's use it! Change the value #$700 to #$777 and run the program again. The background colour will now be white as snow, like we're used to.

In the next tutorial, I plan to cover hexadecimal, binary and decimal numbers, program flow and talk some about traps and registers.


Warrior Munk of poSTmortem, 2002-04-02 "No immovable roots, no stirring of dust, only the tao; the way of nature. This is called tai chi." - Tai Chi Master

Last edited 2002-06-14

proceed to Perihelion tutorial 3