Programming

From C64-Wiki
Jump to: navigation, search
Stub This article is very short and not very detailed. Please help to improve it.

Programming or coding is the combination of a few computer commands (of a computer programming language), which is also known as a program, which is used for controlling a computer system with a definitely function. The person, who do this work, is called a programmer or coder.

Note: Programming isn't easy. The first steps by using a few BASIC commands from your computer manual can be easy, but for developing a complex and great program, you must know a lot of this hard- and software and something more!

At the beginning of the computer aera the first programs was developed and saved on punched card. Since the 1970's the programmers use the computer memory (RAM) with special programs and developing systems like tools, editors, compilers, etc.

The syntax of computer or programming languages are standardised (and also easy learnable). It exists a lot of programming languages like BASIC, Pascal, Comal, machine code or Assembler, etc.

Attention: After writing a program into the computer memory (which is lost all datas after switch-off the computer) must this program first saved on a data memory!

Examples of programs[edit]

BASIC 2.0 Listing for C64[edit]

  • For a C64 in BASIC 2.0
10 REM *** C64-WIKI STAR DEMO ***
15 REM * REM-Zeilen 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 * Randominze 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]

  • 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]

#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]

\ 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]

  • No text prining and changing background color:

center

       ; 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