To use, place an all-user-readable file named 'dis.of' containing this source in the root directory of your ppc Mac's Startup Volume Make sure the backslash beginning the first line is the first. Computer Architecture Instruction Set Architecture. Lynn Choi School of Electrical Engineering. Information Representation. Digital information Instruction (code) A program contains both instructions and data Instruction contains opcode and operands Data Text (alphanumeric codes).
The POWER5 processor is a 64-bit workhorse used in a variety of settings. Starting with this introduction to assembly language concepts and the PowerPC instruction set, this series of articles introduces assembly language in general and specifically assembly language programming for the POWER5. The POWER5 processor is a 64-bit workhorse used in a variety of settings. Starting with this introduction to assembly language concepts and the PowerPC instruction set, this series of articles introduces assembly language in general and specifically assembly language programming for the POWER5. May 28, 2013 Download PearPC - PowerPC Emulator for free. PearPC is an architecture independent PowerPC platform emulator capable of running most PowerPC operating systems. It includes a.
This is the trickiest chapter by far. I will attempt to explain just what a Macintosh is (from a programmers perspective), how it is programmed from a low level point of view and where some potential problems can occur.
The Macintosh, or more lovingly, the Mac, debuted way back round about 1984 (which is about two centuries in computing time). At the time it was heralded as a complete and utter breakthrough. Today, it can be classified as a high powered, multi-media workhorse. Some would argue it is not a multi-tasking system, but from a programmers point of view it is very multi-tasking. The kind of multi-tasking I am talking about is that of being able to run multiple processes sharing a finite amount of hardware resources. For this very reason, even a game can't just ditch the OS and get on with it. For example; you may grab a serial port and start sending data. But if another process running in the background asks for use of the same serial port, the OS has no way of knowing you have grabbed it. The result is garbled data. With the advent of real time systems such as Open Transport Networking and multithreaded processes, even an assembly language programmer has to follow the rules. Granted that an assembly language programmer can do things a C programmer simply can't. Even in this day and age, certain parts of the OS can only be talked to with some assembly language 'glue'. Of course, the assembly language programmer doesn't need this glue, and so gets faster execution.
For the reasons outlined above, you need to know how to talk to to the OS, and what facilities it provides. Fortunately under PowerPC, accessing the OS is far simpler than it is for 68K, where you normally push parameters on the stack, but not always. A case in point is the memory manager when called from 68K code. It generally takes a pointer in a0, and any size data in d0. In PPC, all parameters are passed in registers. The first integer parameter goes in r3, the next in r4 etc. Floating point parameters are passed in f1 onwards. Any budding Mac programmer NEEDS to get hold of Inside Mac Toolbox essentials and Overview. These are totally necessary reading. Believe me, if you break the rules on the Mac, even though your program may run, your users will give you a very hard time :-)
Good, that's the OS introduction out of the way. From the above you will hopefully see why an understanding of the OS is necessary. So what is the MacOS? As noted previously, the Mac has a long and somewhat uneven heritage. Many projects have been started at Apple and only half heartedly incorporated into the OS (PowerTalk, GX printing etc.). Many other projects have been highly succesful and become 'core' - for example the Sound Manager. Thus we have an OS with many 'core' components and many 'not so core' items. This really doesn't matter to us. What technologies you decide to use is up to you. The important thing is that you do need to use some of the OS. Why? There have been many different motherboards, processors and chips used in the Mac over the years. It's not like the Amiga with a standard chipset - you simply don't know what motherboard your pride and joy will end up running on. This may not sound like a whole load of fun, but don't worry - you need to use the OS yes, but you don't need to do everything through it. I know you may be thinking calling the OS is slow because of all those parameters you have to pass and set up. True. The trick is to use the OS just where you really need to, or at least to use the OS to get the machine into the state you need it. Just bear in mind that it is a multitasking environment and all will be fine.
Now to make this easier for all of us, I'm going to show you two ways of calling the OS, and from then on, use the second. First the underlying theory.
On the PowerMac, all OS functions are exported from fragments (typically shared libraries). This means that your PowerPC program needs to be linked (at run time) with the OS exports. Then to call an OS function, your code has to set up the TOC to that of the shared library, find the address of the function, and then branch to it. Of course, it also has to set up a stack frame, and save some important registers (like our toc for example!). Here's the base, raw code to do it:
Replace the text 'the_function' with whatever OS function you are calling. The name of the function must have previously been imported via Fantasms import directive. So, let's take BlockMove as an example - the code to call BlockMove (moves a chunk of memory) would be:
BlockMove is defined as:
BlockMove(srcPtr, destPtr, byteCount);
So, we move our srcptr into r3 (where the data is now), we load r4 with our destptr (where we want the data copied to) and we load r5 with the number of bytes to copy. (As you can see, BlockMove would be better called 'BlockCopy' but there you go...).
So, if we wanted to move 1000 bytes from fred to harry the complete code would be:
As you can see, that's a whole lot of code to type every time we want to call something in the OS! It's also very error prone. So we don't do it that way. You can if you want, but it's not recommended - I just wanted to show you the mechanics.
We roll all the common code into a macro, and use that instead:-
Easier? Course it is. The only requisite is that you must include the right .def file into your project either as a globinc, or include the file into your source file with an includeh directive. If you look in the Anvil folder 'Anvil low level defs' you'll find lots of these files. How do you find the right file? Easy; use Anvil's search all files in folder feature. Open any file from the low level defs folder with Anvil. Now bring up the find dialog and type in 'blockmove' in the find field, next click the 'Search all files in folder' check box and then click OK. Within a few seconds, Anvil will have found the file that contains BlockMove; in this case, 'memory.def'. Def files are Fantasm's low level equivalent of C's header files.
Fantasm 5.1 expands on this concept by removing the need to include the low level def file through the use of zillions of macros - one for each OS function. In the above case, blockmove would be called as:
It doesn't look much different right? True, but what you get is error checking in that the macro knows how many parameters to expect, and so can fail if you pass not enough or too many parameters. Also it means you don't have to get the parameters in the right registers
Would be perfectly acceptable. The macros also provide the same facilities under 68K assembly language too, so you may want to take a look at this when 5.1 is published.
Note about BlockMove - if you only have a small amount of data to copy, do it yourself. By the time you've called BlockMove, you could've done it already. For large amounts of data, BlockMove is hard to beat.
Now we know how to call the OS. Good. You are now asking yourself 'I wonder how many functions are in the OS?' The answer is 'Thousands' and I can pre-empt your next question 'So how do I find out which one I need?' with the following answer.
Download Inside Mac for starters. They are all free from Apple (go to our links page where you'll find links to them). At the back of each chapter is a list of functions described in that chapter along with the parameters. Alternatively, all good booksellers will sell you the volumes in paper form or you can get them all on CD from Apple. If you are really serious about it you may want to buy either Think Reference from MacTech (which is the one we use) or Macintosh Toolbox Assistant from Apple ($89 last time I checked). You just type in the first couple of letters of the function you are interested in and it'll find the function for you. You'll get a description along with things to watch for and of course the required parameters and any return data.
So, what kind of OS functions are you likely to need? Well first off, any application HAS to do certain initialization, otherwise you'll call an OS function and it'll crash. Luckily, to save you time, a library function called InitMac is supplied. Call this at startup and you have no problems.
Make sure you have added the Application library to your project, else it won't link (it'll fail with a 'Where is this InitMac??' error (or words to that affect)).
After this, you are in a position to get on with you program. Nearly every type of program must have a window to draw into. You can't just draw 'anywhere'. You must draw into a graphic port. More often than not these days, you want to use color, so it needs to be a color graphic port - a CGrafPort. Windows can be created in one of two ways - either manually or you can get one from a resource. Either way is fine - you end up with the same result. From a resource is the quickest and least code intensive of the two. If you open Resedit, create a new file and then create a 'WIND' resource, you'll find you can edit your window graphically. Be sure the initially visible checkbox is set and leave the ID at 128 for the purposes of this example. If you now save the file and then add it to your Anvil project; the window you have defined will be copied to your application when built.
The function we need to use to get the window is called GetNewCWindow and looks like this:
pCWindow = GetNewCWindow(windowID, wStorage, behind);
windowID is the resource ID of the window. 128 in our case.
wStorage. You can tell the window manager where to put the window record, or if you set it to null (0) the storage will be allocated for you.
behind is the windowptr (what this call returns) of the window you want this window to appear behind. If you set it to -1, the window is placed at the front.
Now we can load the window with the folllowing code:
After this code, r3 will either contain a valid pointer to the window or zero if the call failed for some reason. You should then store the ptr to the window somewhere safe, such as in a global variable. Note that this call gets the window, but does not set the port. So the next thing we need to do is set the current port.
Note the optimization of storing the window_ptr during the check for a valid pointer. This means that we can store a zero as the ptr (if there was an error), but heck, if we have an error anyway it doesn't really matter. The ptr to the window actually points to the graphics port (CGrafPort) for this window, so if we pass that to SetPort we then have a valid drawing environment:
And to prove it, we can now print something:
Finally, we can wait for the mouse button to be pressed before quitting:
And to quit, we call the macro 'tidy_up' (assuming 'start_up' was called at the start).
If you were to run this program, you'd get a window, and it would quit when you pressed the mouse button, but you wouldn't see any text. Why? Well, we haven't told the OS where we want to draw, so it does it at 0,0 (x,y coords). Because text is printed from the bottom up, our text is drawn out of view at the top of the window. We need to move the drawing coordinates (or the pen position) to a suitable location before drawing:
Now, we will see the text.
One of the things you need to do in any programming language is change the flow of instructions depending on the result of an operation. Consider the change to our program below:
Can you visualize the result of this? Whilst thinking about it, note again, I have placed two instructions at the bottom between the subtract with record instruction (subic.) and the conditional branch (bne). The adds in-between these two instructions are effectively 'free'. The processor would take five cycles to determine whether to take the branch or not, so by opting for a subic. rather than using the counter register (which may at first seem the obvious choice) to control the loop, I have incremented my printing coords 'for free'. Any integer instruction with a dot after it will affect the condition flags field 0. Any conditional branch without a cr field assumes cr0. We could write bne cr0,draw_loop and it would mean the same thing.
Note that a straight subi can't be used to set the cr0 field of the condition code register. One must use a subic.
Anyway, back to the program - it prints ten strings, each offset in x and y slightly. OK, cool. Now how about we want it continually printing these strings until we press the mouse button. What we need to do is: after printing the strings, erase them, check the mouse, and if not pressed reprint them, erase them, check the mouse, etc.
How do we erase them? Well the obvious choice is the OS function EraseRect. It takes a rectangle defined as four 16 bit values of top, left, bottom, right. This definition is pretty much a Mac standard as far as rectangles go, so you may as well get it into your head now. Top, left, bottom, right. You just need to learn it.
Now we could say, OK, I know the size of that window so I can define the rectangle to erase as a constant set of data with a dc.h directive. Wrong :-)Yes, you can do this, but what happens if the user changes the size of your window? If you remember back a few paragraphs, I said that a windowptr is really just a CGrafPort pointer. If we look at the CGrafPort structure, we can find something that is useful. Actually, something that may be of benefit here is to examine just how we find out about a given structure. In this case we are talking graphics. This menas that books like Inside Mac Memory probably won't help much. Inside Mac Toolbox Essentials might as it covers all the really important things, and a graphic ports are pretty important. So, I load up my Inside Mac CD (you can buy them on CD, or if you have a 'burner' you can make your own IM CD after downloading them from Apple. Tip, just write them as a session, that way you can add them to your CD as they are published/updated. So, I load in my IM CD and open TB Essentials.
I go to the WindowManager section and find the defintion of a CWindowRecord. Sure enough, the first entry is the graphics port, but this document doesn't expand on the graphics port structure. It does however tell me that the structure is defined in Inside Macintosh: Imaging. So, now I load that one up, goto the table of contents and immediately find the definition I'm looking for (actually I just type 'cgrafp' into Think Reference :-)).
So, we can see that at offset 16 in the cgrafport is the rectangle (portRect) that defines the current size of the CGrafPort. Thus we should pass the address of this rectangle to the EraseRect function. Now, it doesn't matter what size the window is, we will always erase the whole visible part of it (assuming you haven't messed up the current clipping rectangle, but we haven't come to that yet).
That may sound complicated, but the code is trivial:
See! This will erase the window. Here's a useful MacsBug tip in case you aren't aware of the power of MacsBug. It contains many templates for popular Mac data structures. So, if you have r3 pointing at a CGrafPort, in MacsBug you can type:
And MacsBug will display the grafport with all it's field names and contents. 'dm' is the MacsBug command to display memory. sm is the command the set memory.
Right, so now we can erase the window. All we need to do now is change the mouse button wait loop to branch back to the start of the drawing code rather than just waiting. Here is the program in total as it looks at this stage:
Hopefully, by now you are getting into the swing of calling the OS. I'm not going to dwell on it too much longer. You just have to learn the various functions available. Obviously in the rest of this series we will being seeing more OS functions, but I won't be detailing them too heavily. The Mac OS is big with a capital B. You just have to learn as you go.
NOTE: We used EraseRect to clear the window. We could have used PaintRect to achieve the same result. The difference is that EraseRect is quicker than PaintRect.
The Mac when drawing uses two colors: The forground color and the background color. EraseRect works with the background color. There are two OS calls to set these colors - RGBForeColor and RGBBackColor. These both take a pointer to three 16 bit unsigned values. Each 16 bit value specifies the red, green and blue intensity of the colour. So 0xffff,0xffff,0xffff is white, whilst 0x0,0x0,0x0 is black - red, green and blue. 0xffff,0,0 is max red, 0,0xffff,0 is max green.
Armed with this information we can start doing some funky color stuff. By altering the for and background colors we can alter the color of the text and the background of the window. Suppose we just incremented the value of the red component of the background color on each loop (and the other two components were set to zero). What would be the visual effect? Let's try it and see. We need to define some new data - the background color - we'll call it my_bg_color. Then we need some code after we erase the rectangle to change the background (bg) color:
Of course, the red slowly fades up to maximum red and then swiches back to black. But what would be the result of the following code?
Believe me, you don't want to work through too many steps. The end result is almost a random color fade and switch affect - but of course it isn't random. Best tried on a monitor capable of millions of colors.
And that's it for this chapter. The above code snippet is pretty unoptimized as far as PPC code goes - can you optimize it? Answer next chapter (tip: what if the color component we are loading to modify isn't in the level 1 cache and hence isn't immediately available?). Another question: Can you change the colour changing code so that abrupt color changes do not occur - it's all nice and smooth? Final question: How could we change the background color without erasing (in 256 indexed color mode only) - Tip check in the examples folder of your Fantasm 5 CD?
Don't worry if you find these questions baffling - we'll cover them all next time along with more application goodies such as menus and events with the emphasis being on fun.
Download File Beq Instruction Powerpc For Mac
Powerpc For Mac Os X
The project for this chapter can be downloaded from here (Fantasm 5 project) - 8k.
Copyright Lightsoft 1998.
Reproduction in whole or part prohibited without permission.