Home BMDS: Microtan does Knight Rider
Post
Cancel
Preview Image

BMDS: Microtan does Knight Rider

In my electronics projects I frequently use LEDs when testing outputs and in debugging code, so I thought I would add some to BMDS. The main control form now sports an optional set of 8 LEDs, which are mapped to a configurable address. Whenever the address is written to the LEDs light according to the bit pattern of the value written; 1 = on, 0 = off.

Initially I stuck with a simple blink program but then decided to get ambitious and having enjoyed Michael Knight’s KITT car many years ago, decided to try cycling the LEDs back and forth. Could also be a Cylon, as I like Battlestar Galactica too.

It is a long time since I wrote 6502 code so was pleasantly surprised when it worked first time. I only had to tweak the delay a few times to have it look as I wanted. I guess the decades of programming since my first attempts in the late 70s have taught me something!

The code is fairly simple, using a table of bit masks to select the relevant bit for each LED. I put the LEDs address at $B000 with the other I/O addresses.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Assembler for processor 6502
//
// File "knight_rider_led.asm"
// 04 Jun 2020, 09:29
//

0001                    //
0002                    // Testing LEDs with a Knight Rider like sequence
0003                    //
0004                    // Assemble with 'Write code to memory' set
0005                    // Then in TANBUG use G400 to run
0006                    // Stop with Reset button
0007                    //
0008                    
0009    0100            leds        equ   $B000
0010                    
0011    0102                        org   $400,CODE       // Start of program code
0012                    
0013    0102  A2 00     start       ldx   #0
0014    0104  A9 00                 lda   #0
0015    0106  1D 60 01  loopLeft    ora   ledOn,X         // LED x on
0016    0109  8D 00 01              sta   leds
0017    010C  20 4F 01              jsr   delay
0018    010F  1D 61 01              ora   ledOn+1,x       // then LED x+1 on
0019    0112  8D 00 01              sta   leds
0020    0115  20 4F 01              jsr   delay
0021    0118  3D 68 01              and   ledOff,X        // LED x off, leaving x+1 on
0022    011B  8D 00 01              sta   leds
0023    011E  20 4F 01              jsr   delay
0024    0121  20 4F 01              jsr   delay
0025    0124  E8                    inx
0026    0125  E0 07                 cpx   #7              // Cycle through all LEDs
0027    0127  D0 DD                 bne   loopLeft
0028                    
0029    0129  A9 00                 lda   #0
0030    012B  1D 60 01  loopRight   ora   ledOn,X         // Then do in reverse
0031    012E  8D 00 01              sta   leds
0032    0131  20 4F 01              jsr   delay
0033    0134  1D 5F 01              ora   ledOn-1,X
0034    0137  8D 00 01              sta   leds
0035    013A  20 4F 01              jsr   delay
0036    013D  3D 68 01              and   ledOff,X
0037    0140  8D 00 01              sta   leds
0038    0143  20 4F 01              jsr   delay
0039    0146  20 4F 01              jsr   delay
0040    0149  CA                    dex
0041    014A  D0 DF                 bne   loopRight
0042    014C  4C 02 01              jmp   start           // And around again
0043                    
0044                    //
0045                    // Delay, set by trial
0046                    //
0047    014F  48        delay       pha                   // Save state
0048    0150  8A                    txa
0049    0151  48                    pha
0050    0152  A2 14                 ldx   #20             // Outer loop
0051    0154  A0 00     loop1       ldy   #0
0052    0156  88        loop2       dey                   // Inner loop 256 times
0053    0157  D0 FD                 bne   loop2
0054    0159  CA                    dex
0055    015A  D0 F8                 bne   loop1
0056    015C  68                    pla                   // Restore state
0057    015D  AA                    tax
0058    015E  68                    pla
0059    015F  60                    rts
0060                    
0061                    // Table of bits for switching each LED on or off
0062                    
0063    0160  01        ledOn       fcb   %0000.0001           // On
0064    0161  02                    fcb   %0000.0010
0065    0162  04                    fcb   %0000.0100
0066    0163  08                    fcb   %0000.1000
0067    0164  10                    fcb   %0001.0000
0068    0165  20                    fcb   %0010.0000
0069    0166  40                    fcb   %0100.0000
0070    0167  80                    fcb   %1000.0000
0071                    
0072    0168  FE        ledOff      fcb   %1111.1110           // Off
0073    0169  FD                    fcb   %1111.1101
0074    016A  FB                    fcb   %1111.1011
0075    016B  F7                    fcb   %1111.0111
0076    016C  EF                    fcb   %1110.1111
0077    016D  DF                    fcb   %1101.1111
0078    016E  BF                    fcb   %1011.1111
0079    016F  7F                    fcb   %0111.1111
0080                    
0081                                end

Assembly generated 0 errors and 0 warnings

The ASM file is available in the examples/Microtan 65 folder on GitHub along with the rest of the BMDS source code.

This post is licensed under CC BY 4.0 by the author.
Recently Updated
Trending Tags
Trending Tags