C64
Source: lib/C64.cs
This page is generated from the source by scripts/gen-reference.py.
The Commodore 64: its 40x25 character screen and 16 colors, sprites, the video chip's raster line, the keyboard, a joystick and a clock. Only for programs built with ./build.sh --c64; the functions are in c64/runtime.c, and a program for another target that calls them won't link.
C64.Clear(C64.Black);
C64.Border(C64.Black);
C64.Print(4, 2, "Hello from C#", C64.Yellow); // column, row
while (C64.ReadKey() == 0)
{
C64.WaitFrame(); // 50 times a second
C64.Poke(0xD020, frame++ & 15); // any address at all
}
Out.Line and In.ReadLine work as everywhere, through the C64's own screen editor: text scrolls, and a typed line is handed over at RETURN. Print puts characters at a position without moving that cursor.
Everything here is a few Peeks and Pokes, and each of those costs about 125 processor cycles, of the 20,000 in a frame: a call from C# passes 32-bit numbers through an 8-bit processor. Print loops in C, at about 170 cycles a character.
Members
Columns, Rows
public const int Columns = 40, Rows = 25
Black, White, Red, Cyan, Purple, Green, Blue, Yellow
public const int Black = 0, White = 1, Red = 2, Cyan = 3, Purple = 4, Green = 5, Blue = 6, Yellow = 7
Orange, Brown, LightRed, DarkGray, Gray, LightGreen
public const int Orange = 8, Brown = 9, LightRed = 10, DarkGray = 11, Gray = 12, LightGreen = 13
LightBlue, LightGray
public const int LightBlue = 14, LightGray = 15
Up, Down, Left, Right, Fire
public const int Up = 1, Down = 2, Left = 4, Right = 8, Fire = 16
Peek
public static int Peek(int address)
The byte at a memory address (0-65535).
Poke
public static void Poke(int address, int value)
Writes a byte to a memory address. Nothing stops it writing into the program itself.
Clear
public static void Clear(int background = Blue)
Empties the screen, sets its background color, and puts Out's cursor in the top left corner.
Border
public static void Border(int color)
Sets the color of the frame around the screen.
public static int Print(int column, int row, string text, int color = White)
Writes text at a column (0-39) and row (0-24), without moving Out's cursor. Returns the column after it. Text that reaches the end of a row goes on in the next.
public static void Print(int column, int row, char character, int color = White)
Writes one character at a column and row.
public static int Print(int column, int row, int value, int color = White)
Writes a number at a column and row. Returns the column after it.
Sprite
public static void Sprite(
int sprite,
int slot,
ReadOnlySpan<byte> shape,
int color)
Gives sprite 0-7 a shape and a color and shows it. The shape is 63 bytes: 21 rows of 24 pixels, three bytes a row, the leftmost pixel in the highest bit. There is room for three shapes (slot 0-2); sprites can share one.
MoveSprite
public static void MoveSprite(int sprite, int x, int y)
Moves a sprite. Its top left corner is at the screen's top left corner when x is 24 and y is 50; x goes up to 343, y to 249.
HideSprite
public static void HideSprite(int sprite)
Hides a sprite.
WaitLine
public static void WaitLine(int line)
Waits until the video chip starts drawing a line of the picture (0-255; the screen's 200 lines are 51-250). Changing a color then changes it from that line down: that is how raster bars are made.
WaitFrame
public static void WaitFrame()
Waits for the picture's lower border: once per frame, 50 times a second (60 on an American machine), and the moment to move things without flicker.
Ticks
public static uint Ticks
Sixtieths of a second since the machine was switched on.
ReadKey
public static int ReadKey()
The next key pressed, as its character ('a', 'A', '1', ' ', '\n' for RETURN), or 0 if no key waits. Keys without an ASCII character come as the C64's own codes (cursor down is 17, F1 is 133).
KeyHeld
public static bool KeyHeld(char key)
Whether a key is held down right now: a letter (either case), a digit, ' ' or a sign on the keyboard. Unlike ReadKey this doesn't wait for a press, so a game can move something for as long as a key is held. One key at a time: of two held together it sees one. Shift, Ctrl and C= don't count.
Joystick
public static int Joystick
The joystick in port 2: Up, Down, Left, Right and Fire, added up; 0 when it is at rest.