Programming

From C64-Wiki
Jump to navigationJump to search
Demo: Start screen with short programming of a BASIC program.

Programming, or coding, involves combining commands from a computer programming language to create a program that instructs a computer system to perform a specific function. The person who does this work is called a programmer or coder.

Note: Programming can be difficult. The first steps, such as using a few BASIC commands from your computer manual can be easy, but for developing a complex program it's often necessary to understand much more of the underlying aspects of both hard- and software.

In the early days of digital computing, programs were developed and saved onto paper tape or punched cards. Since the 1970s, with ever-expanding memory (RAM) and storage such as floppy and hard disks, many tools have become available to assist the programmer, such as editors and compilers, etc.

The syntax of any given computer or programming languages is standardised (and generally easy learnable). There are many different programming languages, such as BASIC, Pascal, Comal, machine code or Assembler etc.

Attention: After writing a program into the computer's memory (which loses all data when the computer is switched off), it is essential to save the program to a data memory to ensure it is not lost.

Examples of programs[edit | edit source]

BASIC 2.0 Listing for C64[edit | edit source]

  • For a C64 in BASIC 2.0
10 REM *** C64-WIKI STAR DEMO ***
15 REM * REM-lines aren't needed! *
20 PRINT CHR$(147); "!!! THE C64-WIKI STAR DEMO !!!"
25 REM * Setting up frame and background color (start with COL=0 )
30 POKE 53280,COL : POKE 53281,PEEK(53280)
35 REM * Randomize value in variable COL
40 LET COL = INT( RND(1) *16)
45 REM * Randomize text color with COL
50 POKE 646, COL
55 REM * Randomize position of cursor
60 POKE 211, INT( RND(1) *40)): POKE 214, INT( RND(1) *24)): SYS 58640 
65 REM * Print a star on monitor
70 PRINT "*";
75 REM * 1/25 chance for changing border color 
76 REM * back to line 30
80 IF RND(1) < 1/25 THEN GOTO 30
85 GOTO 40: REM back to line 40
90 REM *** STOP ONLY WITH RUN/STOP KEY !!! ***
95 END: REM * END isn't needed! *

Compressed Listing, bad programming style[edit | edit source]

  • Shorted and memory saved program for a C64 (same program routine!):
1 PRINT"{clr}!!! THE C64-WIKI STAR DEMO !!!":GOTO4
2 C=INT(RND(1)*16):POKE 646,C:POKE211,INT(RND(2)*40):POKE214,INT(RND(3)*24)
3 SYS58640:PRINT"*";:IFRND(1)>=.04THEN2
4 POKE53280,F:POKE 53281,F:GOTO2
  • Second variant - more faster and shorter:
1 print"{clr}!!! the c64-wiki star demo !!!":s=4^5:c=s*54:d=53280:e=d+1
2 f=rnd(1)*16:o=rnd(1)*1e3:pokec+o,f:pokes+o,42:ifrnd(1)>=.04then2
3 poked,f:pokee,f:goto2

Listing in C for cc65[edit | edit source]

#include <stdlib.h>
#include <conio.h>

void main(void)
{
    unsigned char breite, hoehe;
    unsigned char hintergrund_alt, rahmen_alt, text_alt;
    unsigned char farbe = COLOR_BLACK;
   
    _randomize();
    screensize(&breite, &hoehe);
    clrscr();
    hintergrund_alt = bgcolor(farbe);
    rahmen_alt = bordercolor(farbe);
    text_alt = textcolor(COLOR_YELLOW);
    cputs("!!! Das C64-Wiki Sterne Demo !!!");
    while (! kbhit()) {
        farbe = rand() % 16;
        textcolor(farbe);
        cputcxy(rand() % breite, rand() % hoehe, '*');
        if (rand() % 100 <= 1) {
            bgcolor(farbe);
            bordercolor(farbe);
        }
    }
    cgetc();
    bgcolor(hintergrund_alt);
    bordercolor(rahmen_alt);
    textcolor(text_alt);
    clrscr();
}

Example in Forth[edit | edit source]

\ Random Numbers

\ The algorithm is ported from cc65.

| create seed 5 allot

: seed! ( n -- )
  seed 5 erase  seed 3+ ! ;

: seed@ ( -- n ) seed 3+ @ ;

: random ( -- n )
  seed 1+ 2@  seed 2@ d+
  $3.1415927 d+  seed 1+ 2!  seed 2+ @ ;

: randomize ( -- )
  $d012 c@ 256 *  $a0 c@ or  seed! ;

: randint ( max -- n ) random swap mod ;

1 seed!


\ Star Demo Main

-1 +load  \ random

: random-color ( -- n ) 16 randint ;
: text-color! ( n -- ) 646 c! ;
: border-color! ( n -- ) 53280 c! ;
: background-color! ( n -- ) 53281 c! ;

: star demo ( -- )
  randomize
  page ." !!! C64-Wiki star demo !!!"
  begin
    random-color
    dup text-color!
    l/s randint  c/l randint  at
    ascii * emit
    100 randint 2 <
    if
      dup border-color!
      background-color!
    else drop then
  stop? until
  cold ;

Example in Assembler[edit | edit source]

Demo of this assembler program.
  • No text printing and changing background color:


       ; Program: Sternenhimmel
       ; Start: SYS 49152
       ; ACME:   "acme.exe -o sterne.prg -f cbm sterne.asm" 
       ; 64TASS: "64tass.exe -o sterne.prg sterne.asm"

       *= $c000         ; Startadresse 49152 oder $c000
start  lda #147         ; Bildschirm löschen
       jsr $ffd2

       lda #$00         ; Bildschirm schwarz
       sta $d020
       sta $d021

       lda #$81         ; SID-Tongenerator - Stimme 3
       sta $d412        ; Wellenform = Rauschen

       lda $d012        ; aktuelle Rasterstrahlposition auslesen (SEED)
       ora %10000000    ; MSB setzen, für hohe Frequenzen
       sta $d40f        ; oberes Frequenz-Byte setzen - Stimme 3

                        ; Zufallszahl $0400...$07e7 bzw. $d800...$d8e7    
endlos ldx $d41b        ; Zufallswert holen (Oszillator3)
       stx $fb          ; Bildschirmposition Low-Byte
       stx $fd          ; Farb-Ram-Position Low-Byte

       lda $d012        ; Zufallswert holen (Rasterstrahlposition)
       and #%00000011   ; Wertebereich $00-$03 
       ora #%00000100   ; Wertebereich $04-$07
       sta $fc          ; Bildschirposition High-Byte $04-$07

       and #%00000011   ; Wertebereich $00-$03
       ora #%11011000   ; Wertebereich $d8-$db
       sta $fe          ; Farb-Ram-Position High-Byte $d8-$db

       cmp #%11011011   ; wenn Wertebereich 
       bne weiter       ;      $dbe8...$dbff
       cpx #$e8         ; bzw. $07e8...$07ff
       bcs endlos       ; dann wiederholen

weiter ldx #$00
       lda #'*'         ; Sternchen in      
       sta ($fb,x)      ; Bildschirmspeicher schreiben

farbe  lda $d41b        ; Zufallswert holen (Oszillator3)
       and #%00001111
       beq farbe        ; wenn schwarz, dann nochmal
       sta ($fd,x)      ; zufällige Farbe 1-15 ins Farbram schreiben 

taste  jsr $ffe4        ; Taste gedrückt?
       beq endlos       ; nein -> Sprung nach endlos
       rts              ; ja -> Rücksprung ins BASIC