Skip to content

Find the wrong note

Bright Spark · Optional investigation · Spectrum 48K BASIC

The panels light in the right order, but one note is wrong. We’ll find where its value first differs from our expectation.

Start after Give a choice a signal. We need its variables, IF conditions and nested GO SUB calls. We do not need to identify notes by ear: a temporary number display gives us another way to investigate.

Keep a working copy

Save the working unit 02 program first. Use a separate emulator session, or clear BASIC with NEW (A at the command prompt, then Enter) before entering the complete example below. Adding it over the working program would leave an old line in memory and hide our fault. Do not save this experiment over spark.

Deliberately broken program — investigation only
broken.bas
  10 BORDER 0
  20 PAPER 0
  30 INK 7
  40 CLS
  50 PRINT "BRIGHT SPARK"
 100 LET lit=0
 110 FOR p=1 TO 4
 120 GO SUB 500
 130 NEXT p
 240 PRINT AT 2,7;"1"
 250 PRINT AT 2,23;"2"
 260 PRINT AT 11,7;"3"
 270 PRINT AT 11,23;"4"
 280 PRINT AT 20,0;"Watch panels 1 to 4."
 300 FOR p=1 TO 4
 310 GO SUB 900
 320 NEXT p
 330 STOP
 500 LET pr=3
 510 LET pc=2
 520 LET colour=2
 530 LET inkcol=7
 540 LET note=0
 550 IF p=2 THEN LET pc=18
 560 IF p=2 THEN LET colour=1
 570 IF p=2 THEN LET note=4
 580 IF p=3 THEN LET pr=12
 590 IF p=3 THEN LET colour=4
 600 IF p=3 THEN LET inkcol=0
 620 IF p=4 THEN LET pr=12
 630 IF p=4 THEN LET pc=18
 640 IF p=4 THEN LET colour=6
 650 IF p=4 THEN LET inkcol=0
 660 IF p=4 THEN LET note=12
 670 PAPER colour
 680 INK inkcol
 690 BRIGHT lit
 700 FOR r=pr TO pr+5
 710 PRINT AT r,pc;"            "
 720 NEXT r
 770 IF lit=1 THEN PRINT AT pr+2,pc+5;"*"
 780 PAPER 0
 790 INK 7
 800 BRIGHT 0
 810 RETURN
 900 LET lit=1
 910 GO SUB 500
 920 BEEP 0.15,note
 930 LET lit=0
 940 GO SUB 500
 950 PAUSE 10
 960 RETURN

Predict, then investigate

Run with no keys held. Each run starts by drawing four resting panels, then requests panels 1, 2, 3 and 4. We expect pitches 0, 4, 7 and 12, each lasting 0.15 seconds, with each asterisk disappearing afterwards.

Before running, trace one call from line 310 through 900 and 500 to 920. Write down the four pitch values we think will actually reach BEEP. Run it and describe expected and actual behaviour. Which parts still work?

Here is the broken run captured from the 48K PAL emulator. Listening is optional; a correct-looking panel is not proof of a correct pitch.

Choose a specific hypothesis before editing: is the wrong value already in note when line 920 begins, or does it change during sound playback? Predict a value that would support the first explanation and one that would contradict it.

Hint 1 — where could we observe the value?

Line 910 calls the renderer before line 920 sounds the note. Add this temporary observation between them:

observe.bas
 915 PRINT AT 21,p*6;note

Run again. Row 21 now shows one number per panel, from left to right. Compare them with 0, 4, 7 and 12. This extra printing costs time; use it to inspect values, not to judge the original rhythm.

Hint 2 — which assignments run?

Follow line 540 and the conditions after it with p=3. Which assignment supplies the default note? Which instruction should replace that value for this panel? A previous panel's value does not survive an unconditional assignment.

Make the smallest repair we can justify. Before opening the explanation, say why it should fix the faulty panel without changing the other three.

Repair and explanation — open after investigating

The diagnostic row reads 0, 4, 0, 12. Panel 3 is the first mismatch: we expected 7, but it already contains 0 before BEEP. Line 540 resets note to zero on every renderer call. The special assignment for panel 3 is missing. It therefore uses the default, rather than retaining panel 2's pitch 4.

repair.bas
 610 IF p=3 THEN LET note=7

Run with the observation still present: the row should now read 0, 4, 7, 12. Check all four panels, not only the repaired one. Changing the default at line 540 would also change panel 1; altering BEEP would obscure the missing selection rule.

Check the repair and put the tools away

Delete the temporary line by entering 915 on its own and pressing Enter. Run twice. Each run should show the original order, four rising pitches and no remaining asterisk. Compare the program with the complete working checkpoint in unit 02: after repair and removal of the observation, they should match.

Explain which observation separated our two hypotheses and why checking only the picture would have missed this fault. Then return to the saved working program. The experiment has not changed the version we keep.