--- title: "Sine Wave" id: 57275 type: "computer_media" slug: "sine-wave" url: "http://localhost/computer_media/sine-wave/" markdown_url: "http://localhost/computer_media/sine-wave.md" published_at: "2024-10-04T07:58:26+00:00" modified_at: "2026-04-03T07:58:38+00:00" author: "David Anderson" featured_image: url: "http://localhost/wp-content/uploads/2024/10/163_Sine.png" excerpt: "A compact sine curve plotter that precomputes 64 sample points and pauses for a keypress — revealing a neat busy-wait input technique worth studying." category: - name: "Archived Media" slug: "archived-media" taxonomy: "category" url: "http://localhost/category/archived-media/" post_tag: - name: "Downloadable" slug: "downloadable" taxonomy: "post_tag" url: "http://localhost/tag/downloadable/" - name: "TS 1000" slug: "ts1000" taxonomy: "post_tag" url: "http://localhost/tag/ts1000/" model: - name: "Timex/Sinclair 1000" slug: "ts-1000" taxonomy: "model" url: "http://localhost/model/ts-1000/" genre: - name: "Demo" slug: "demo" taxonomy: "genre" url: "http://localhost/type/demo/" media_contents: - id: 56734 title: "Timex Sinclair Public Domain Library Tape 1003" type: "computer_media" url: "http://localhost/computer_media/timex-sinclair-public-domain-library-tape-1003/" media_type: "Program" mediadate: "198x" images: - url: "http://localhost/wp-content/uploads/2024/10/163_Sine.png" media_type_tags: "Demo" --- This program plots a sine curve on the screen using the low-resolution PLOT command. It precomputes 64 sample points of a full sine wave cycle into array C, scaling the values to span a vertical range centred around y=22 with an amplitude of 20 pixels. After plotting, it calls a subroutine at line 1000 that busy-loops until a key is pressed before returning, effectively pausing execution twice. Six blank PRINT statements then scroll the display downward. *** ## Program Analysis ### Program Structure The program is divided into three logical phases: 1. **Initialisation (lines 10–40):** Allocates a 64-element array `C` and fills it with precomputed sine values. 2. **Plotting (lines 100–145):** Iterates over the array, PLOTs each point, waits for two keypresses via subroutine, then scrolls the display with blank PRINTs. 3. **Keypress wait subroutine (lines 1000–1010):** Busy-loops on `INKEY$` until any key is held down, then RETURNs. ### Sine Wave Calculation Line 30 computes each sample as: `C(V) = 22 + 20 * SIN((V-1)/32 * PI)` With `V` running from 1 to 64, the argument `(V-1)/32 * PI` sweeps from 0 to just under 2π, producing one complete cycle. The vertical centre is at pixel row 22 and the amplitude is ±20 pixels, giving a total excursion of 40 pixels. Precomputing into array `C` avoids recalculating the sine for each plot, which is a sensible optimisation given the expense of trigonometric evaluation. ### Plotting Loop Lines 100–115 step through all 64 array entries and call `PLOT G-1, C(G)`, offsetting `G` by 1 so x coordinates run 0–63. The curve therefore occupies the left quarter of the 256-pixel-wide display. No `DRAW` is used between points, so the curve is rendered as discrete dots rather than a connected line; at 64 samples this is generally dense enough to appear continuous. ### Keypress Wait Subroutine The subroutine at lines 1000–1010 implements a busy-wait loop: - Line 1000 checks `INKEY$`; if it is not empty (a key is pressed), it RETURNs immediately. - Line 1010 loops back to 1000, spinning until a keypress is detected. This subroutine is called twice in succession (lines 117 and 125), requiring two separate keypresses before execution continues. This is a common idiom for requiring a deliberate key-press-and-release cycle, since a single call might return on the same keypress that triggered any previous action. ### Notable Techniques - Precomputing into a `DIM` array separates data preparation from rendering, a good structural practice even in short BASIC programs. - The double `GOSUB 1000` ensures the user must press a key once to acknowledge and once to proceed, guarding against accidental continuation. - The six blank `PRINT` statements (lines 135–145) scroll any text area output without clearing the graphics area, providing visual separation. ### Anomalies and Dead Code | Line | Issue | | --- | --- | | `2000` | `STOP` is never reached; execution halts within the keypress loop or falls through the blank PRINTs into an implicit end. | | `2020` | `RUN` after `SAVE` would restart the program, but this line is only reached as part of the save sequence and is not part of the normal runtime flow. | ## Source Code ``` 1 REM "SIN CURV" 10 DIM C(64) 20 FOR V=1 TO 64 30 LET C(V)=22+20*(SIN ((V-1)/32*PI)) 40 NEXT V 100 FOR G=1 TO 64 110 PLOT G-1,C(G) 115 NEXT G 117 GOSUB 1000 125 GOSUB 1000 135 FOR H=1 TO 6 140 PRINT 145 NEXT H 1000 IF INKEY$<>"" THEN RETURN 1010 GOTO 1000 2000 STOP 2010 SAVE "1016%3" 2020 RUN ```