Skip to content

Let’s make the screen flash!

This program will make the screen flash on the Commodore 128, and it will also run on the Commodore 64. The background color of the screen is stored at D021 (53281), so this effect is accomplished by manipulating the value at that address, multiple times. This program cannot be written in Basic, because the Basic interpreter is too slow.

Remember that the machine code monitor expects code in one format, and displays it in another, so what I show here, is what I actually type in. I will write my program at location 1000 (4096) in the memory. Type MONITOR to enter the machine code monitor, and type F 1000 1100 0 to create some free space.

To begin writing code, type . directly followed by the address you want to add an instruction to, followed by the instruction and the desired parameters. This stores what you just typed in, and expects you to type in the next command, or just hit Enter to exit the edit mode.

.1000 LDA #$01
STA $D021
NOP
NOP
NOP
LDA #$0B
STA $D021
NOP
NOP
NOP

(and so on…)

Let the final command in your program be JMP $1000.

Press Enter to exit edit mode, type X and Enter to exit the monitor. Start your program by typing SYS 4096. Your screen should now be flashing hysterically!

On the first row, .1000 tells the Commodore 128 machine code monitor that I want my program to start at address 1000 (4096). LDA loads a value to the accumulator, and STA stores current value in the accumulator to the given memory address, D021 (53281). The specific memory address D021 controls the background color. NOP means “no operation” and is used only to make the effect less hysterical. Finally, JMP jumps to the given address, creating an infinite loop. Who needs fireworks now?

Categories: Geeky.

Tags: ,

Positioning sprites on the Commodore 64

The task of positioning sprites on the C64 holds one particular oddity that I want to show. To be able to position a sprite on screen, you must master binary logic on the C64, and binary logic works in the same way as on the Commodore PET, shown here. Also, you need a visible sprite. The following code does the trick. If you don’t own a Commodore 64 and don’t have an emulator, the code also works in JaC64, an on-line C64 emulator implemented as an Java Applet. However, I can’t get the cursor keys to work in JaC64, so don’t mistype anything! ;-)

10 FOR I=0 TO 62
20 POKE 704+I, 255
30 NEXT I
40 POKE 2040, 11
50 POKE 53287, 1
60 POKE 53248, 200
70 POKE 53249, 200
80 POKE 53269, 1

The Y position of the sprite is stored at D001 (53249). You have 256 possible Y positions, but the visible area of the screen is only 200 pixels high. All you need to make the sprite cycle over the screen is some iteration that copies the values 0 to 255 to the D001 address.

The X position however, is a bit more tricky. The X coordinate is stored at D000 (53248). But we need more than 256 possible positions to be able to place the sprite on the right side on the screen. Commodore did not waste a whole byte on solving this problem. At D010 (53264), the first bit keeps track on if D000 holds a value that represent the left part of the screen, or the right part of the screen for the first sprite (sprite 0). We have eight sprites and eight available bits at D010, so the second bit keeps track on if D002 (53250) holds a value that represent the left part of the screen, or the right part of the screen for the second sprite (sprite 1), and so on. So this is what we must do to make the sprite we displayed in the above program run over the screen:

Iterate from 0 to 255, set the value to D000 (line 10-30). Set the first bit at D010 to 1 (line 40). Iterate from 0 to 255, set the value to D000 (line 50-70). Restore the first bit at D010 to 0 (line 80). Repeat from the beginning (line 90).

Type NEW to delete your current program.

10 FOR I=0 TO 255
20 POKE 53248, I
30 NEXT
40 POKE 53264, PEEK(53264) OR 1
50 FOR I=0 TO 255
60 POKE 53248, I
70 NEXT
80 POKE 53264, PEEK(53264) AND 254
90 GOTO 10

If you ran the first program, and typed in this program correctly, you should see a sprite that flies from the left to the right across your screen.

Categories: Geeky.

Tags:

PowerShell functions argument catching

PowerShell is not very object oriented. The support for creating classes is implemented through the Add-Type cmdlet that takes C# code. Here I show how to use Add-Type to define and expose classes that use any part of the .NET Framework. These classes can then be instantiated using the New-Object cmdlet.

Functions are defined using the function keyword. This code defines a function that doesn’t require any arguments:

#Define a function.
function DoSomething()
{
   Write-Output "Hello!"
}

#Call a function.
DoSomething

When defining parameters, you can choose what parameters that need to be strongly typed by adding the desired type name in front of the parameter. Here I have 3 calls to the TwoParams function. The first parameter can be anything, but the second must be and int. Both call 1 and call 2 will run, becase $a can accept a string or an int. Call 3 will fail because $b cannot be a string.

function TwoParams($a, [int]$b)
{
	$b = $b + 1
	Write-Output "a=$a"
	Write-Output "b=$b"
}

#Two correct calls.
Write-Output "Call 1"
TwoParams "Hello!" 10

Write-Output "Call 2"
TwoParams 20 20

Write-Output "Call 3 will fail!"
TwoParams 30 "Hello!"

Default values can be provided, so that omitted arguments still will have a value. In this case, it is assumed that the values 1 and 2 is passed to the function. All parameters are passed by value.

function TestingDefaults($a=1, $b=2)
{
	Write-Output "$a $b"
}

TestingDefaults

Now, let’s look at the strange things. All arguments that are sent in, but not declared in the function head, will be captured by the $args array. In this case, $args in SomeFunction will contain 4 elements, because 4 arguments are sent:

function SomeFunction()
{
	$NumberOfArguments=$args.length
	Write-Output "You gave me $NumberOfArguments arguments."
}

SomeFunction "10" "Hej" 5 10

But here, still 4 arguments are sent, but the first argument is declared, so that only 3 arguments are catched by the $args array:

function SomeFunction($x)
{
	$NumberOfArguments=$args.length
	Write-Output "You gave me $NumberOfArguments arguments."
}

SomeFunction "10" "Hej" 5 10

Categories: PowerShell.

Ny tidning mot religion och vidskepelse!

Följande text är hämtad från Religiogustos. Tidningen har bra skribenter, varav två kommer från Skeptikerpodden. Stöd denna skeptiska aktion genom att beställa en prenumeration!

Bright är ett ideellt projekt mot religion och vidskepelse

Prenumerera genom att sätta in 150 kr (4 nr) på
Pg 593068-0 eller Bg 737-3061
Glöm inte att ange namn och adress
Kontakta oss på mail:
prenumeration@brightmagasin.se
redaktion@brightmagasin.se

I första numret av Bright kan du läsa om:

HBT, Ateism, Vidskepelse, Riddarskolan, Rondellhunden, Katolska kyrkan, Anekdotbevisning, Krucifix i klassrum, Litteratur, Rättegångsgudstjänster, En kvinnokonferens i Kairo, Egyptens hotade kulturarv, Religionskritiska pionjärer, Kyrkans grepp över skolan, Skapelsetroende NO-lärare, Kulturrelativism inom RFSU, Kåserier och essäer samt om en hel del annat…

Mer: http://camillagrepe.blogspot.com/2011/04/ny-tidning-mot-religion-och-vidskepelse.html#ixzz1Iaaf6yeP
http://www.dagen.se/dagen/article.aspx?id=255409
http://www.dagen.se/dagen/article.aspx?id=255361
http://www.dn.se/kultur-noje/scen/south-parks-skapare-gor-musikal-av-mormons-bok

Categories: General.

Tags:

Sprites on the Commodore 64

Things gets a bit tougher on the Commodore 64, because this excellent piece of hardware did not come with Basic equipped with sprite commands. Nor has it a sprite editor or a machine code monitor. However, if we have access to a Commodore 128, sprites can be edited when in C128 mode, and then used in C64 mode. If we want, we can also use the machine code monitor in C128 mode and then execute the program in C64 mode. I am going to display a sprite without using the C128 mode.

The bit pattern at D015 gets and sets what sprites are enabled, just as it does on the Commodore 128, but on the Commodore 128 we also have Basic commands for this. This means that the first sprite (sprite 0) is activated using:

POKE 53269, 1

Still, you might not see anything on the screen. You can position the sprite (sprite 0) by writing the desired X coordinate in D000 and the desired Y coordinate in D001. This will position it at 100, 100:

POKE 53248, 100
POKE 53249, 100

The 0, 0 position is just above and to the left of the visible area, so if it is located at its home position, the sprite is still invisible. Also, the sprite might be empty. Run this program to make the sprite (still sprite 0) totally filled with pixels and displayed in red color. Note that the first line of code (10) tells the Commodore 64 that we want to use 02C0 to 02D7 (704 to 727) for sprite 0 data.

10 POKE 2040, 11
20 FOR I=0 TO 62
30 POKE 704+I, 255
40 NEXT I
50 POKE 53287, 2

Your screen should now look something like this:

When taking this further, it is nice to have a Commodore 64 memory map available.

Categories: Geeky.

Tags:

Collision detection

A nice and quick way to create two filled sprites (sprite 1 and 2) is to enter the monitor and type:

F 0E00 0E3E FF
F 0E3F 0E7E FF

Of course you can fill both at once, but this the above code nicely declares the two memory areas I am using in this example. Now, let’s write a simple Basic program to fire the two sprites so that they collide now and then. This will make one sprite white and the other cyan, and fire the movements.

10 SPRITE 1, 1, 2, 0
20 MOVSPR 1, 100, 100
30 MOVSPR 1, 90#1
40 SPRITE 2, 1, 2, 0
50 MOVSPR 2, 100, 115
60 MOVSPR 2, 270#1

To detect collision between the two sprites, we can use the BUMP function. BUMP(1) returns a bit pattern telling us what sprites has collided, and BUMP(2) returns a bit pattern that tells us what sprites has collided with static graphics on screen. We would expect BUMP(1) to return 3 if sprite 1 and 2 is colliding, given that no other sprites are colliding as well, because the binary number 00000011 represents the decimal number 3. And indeed. Add these two lines to your program to see what BUMP(1) returns:

70 PRINT BUMP(1)
80 GOTO 70

To interrupt the program, press RunStop. If you are using the VICE emulator, that key might be mapped to the Escape key. BUMP(1) is actually just returning the bit pattern in the byte located at D01E, so BUMP(1) is equivalent to PEEK(53278).

Categories: Geeky.

Tags:

Poking graphics on the C128

The skills of using PEEK, POKE and logical operators that I write about for the Commodore PET here, can be used to create graphics on the Commodore 128, in 40-column mode. Before you begin, you might want to clear the screen using the SCNCLR command. Odd name, I know.

The C128 has a built in sprite engine. A sprite is a 2 dimensional area with its own memory area that can be handled separately from the rest of the screen. The C128 sprite engine supports eight sprites. Use the SPRITE command to activate a sprite. Here, I have activated the first of eight sprites:

SPRITE 1, 1, 1, 0

The first sprite is now activated. The default position is up to the right of the screen. However, the sprite might be invisible. If there is junk data in the address space of the sprite, you will see some obscure pixels. The sprite data is located in the memory at 3584 (0E00). Each sprite is 3 bytes (or 24 pixels) wide and 21 pixels high. Each sprite consumes 63 bytes. This information is enough to clear the sprite. In the machine code monitor, use In the machine code monitor, use M 0E00 0E3E to see the sprite data. Note that the monitor displays eight bytes at a time, so it will actually display 0E00 to 0E3F. To clear the sprite, type:

F 0E00 0E3E 0

Leave the monitor using X. Make sure the sprite is visible by typing in SPRITE 1, 1, 1, 0 again. Now, use the POKE statement to control which pixels should be visible and which should be hidden, according to the rules I demonstrated here.

Categories: Geeky.

Tags:

Accessing individual bits on a Commodore PET

On a Commodore PET, each byte that can be read using the PEEK function, and set using the POKE statement. A byte consist of 8 bits, and represent a number between 0 and 255. This code writes the value 5 to the memory address 1020, and then reads it back. The output is of course 5.

POKE 1024, 5
PRINT PEEK(1024)

The POKE statement affects all eight bits at the given address (1024), but bits can be filtered out using logical operators AND and OR. The first (from right to left) of the eight bits represents the value 1, the second bit represents 2, the third 4, the fourth 8, the fifth 16, the sixth 32, the seventh 64 and the last bit represents the value 128.

Since the value 5 is stored at 1024, you would expect all the bits being 0 except the first and the third, since 1 + 4 equals 5. This code reads the first four bits at 1024, and the output is 1, 0, 4 and 0. 1 because the first bit (representing 1) is set, 0 because the second isn’t set, 4 because the third (representing 4) is set and 0 because the fourth isn’t set.

PRINT PEEK(1024) AND 1 : REM Read bit 1 - (1 if set)
PRINT PEEK(1024) AND 2 : REM Read bit 2 - (2 if set)
PRINT PEEK(1024) AND 4 : REM Read bit 3 - (4 if set)
PRINT PEEK(1024) AND 8 : REM Read bit 4 - (8 if set)

Multiple bits can also be read. This code reads both the first and the third bit on the address 1024:

PRINT PEEK(1024) AND 5

The result will be 0 if neither is set, 1 if the first is set, 4 if the second is set and 5 if both the first and third is set. The bit pattern of the value is 00000101, so the bits that are pointed out are the first and the third (from right to left).

Also, you can set individual bits. OR [n] turns a bit on, where [n] is the value that the bit represents. 1, 2, 4, 8, 16, 32, 64 or 128. This code turns on the last bit at 1024:

POKE 1024, PEEK(1024) OR 128

The bit pattern at 1024 is now 10000101. The value that is stored at 1024 is 133, because 1 + 4 + 128 equals 133. To turn the last bit (128) back to zero, use AND together with bit pattern that contains a one at all positions that you want the existing flag to remain, and 0 at the positions you want to turn bytes off. To turn the last bit from the right off, you would want a bit pattern that looks like this: 01111111. All bytes remains, but the last bit from the right is 0. The number 127 has this pattern.

POKE 1024, PEEK(1024) OR 127

Now, the bit pattern at 1024 is changed back from 10000101 to 00000101. The value that is stored in the byte at 1024 is now 5 again.

What can be done with this knowledge?

Categories: Geeky.

Tags:

Visual Basic: The coolest code editor you ever saw

Let’s say that you’re typing a word.

As you type…

…the IntelliSense filters out the word you are typing.

If you type something that isn’t in the list, the list is closing.

If you decide that the last character you typed is wrong, use backspace to delete it. Instantly, the IntelliSense is displayed again, with the possible option selected.

The C# editor has a behavior that is similar to this, but if you go pass an membership access operator, or something like that, the C# IntelliSense is completely lost. You can bring it up again by deleting the line, and start typing again, or by pressing Ctrl+J. The Visual Basic editor picks up automatically, as shown above, whenever you press backspace to delete a word.

Another big advantage in the Visual Basic editor is the enumeration awareness. The Visual Basic editor recognizes when an enumeration is an available option, and provides possible options from the right location in the framework. The C# editor seems very unaware of enumerations, and you might have to look up enumerations that you are unfamiliar of, in the Object Browser by pressing Ctrl+Alt+J.

Demonstrated here:

Categories: Visual Studio 10.

Visual Basic: Serious error trapping

The concept of filtering by exception type is familiar to any C# programmer. Visual Basic takes this one step further by also allowing a logical condition that also needs to be true for the block to be executed. If this feature is used, you can have one Try block with multiple Catch section that filters on the same exception class. This is not possible in C# and not allowed in VB without this extra logical condition. The result is better readability and a more logical structure.

Imagine that you want to do an iteration, that fails after three errors. This is the ugly version:

//Do something 10 times, bail after 3 errors.
for (int x = 0; x < 10; x++)
{
   try
   {
      Console.WriteLine("Try something.");
      //This operation will fail.
      throw new Exception();
   }
   catch (Exception ex)
   {
      Console.WriteLine("Failed.");
      if (x >= 2)
      {
         Console.WriteLine("Too many fails. Bail!");
         Console.ReadLine();
         return;
      }
   }
}

And this is the Visual Basic version. Note the When keyword after the Catch keyword:

'Do something 10 times, bail after 3 errors.
For X As Integer = 0 To 9
   Try
      Console.WriteLine("Try something.")
      'This operation will fail.
      Throw New Exception()
   Catch ex As Exception When X < 3
      Console.WriteLine("Failed!")
   Catch ex As Exception
      Console.WriteLine("Too many fails. Bail!")
      Return
   End Try
Next

The way it should be.

Categories: Visual Studio 10.