Advice wanted: 1 bit analog-to-digital sampling from the cassette port
audio input

TLDR: Can you use the cassette port audio input as a one bit digital to
analog converter?

I have a project that has been on my bucket list since I got my
Model T, and I really want to move it from "TO DO" to "DONE".

For the first part of this project I would like to use the
cassette port audio input as a one bit analog-to-digital
converter. I would like to ask the group for help to determine
if this will work the way I think it works.

I'd like to get some feedback on this technique, not so much
for coding style or bugs still in the code but whether or not
this whole one-bit analog-to-digital scheme will even work
the way I think the cassette port works.

Since this technique uses the cassette port input and it is
very highly timing dependent, I can't just run it on the
Model T simulators, and because "reasons" I can't experiment
on my own Model T right now so I want some advice.

Background

Normally, the way the ROM reads data bits from the cassette
port is that you send the cassette audio into the port on the
back and the audio goes through a little circuit to square up
the waveform. The signal then goes to the 8085 chip SID pin and
the 8085 decides if the incoming signal on the SID pin looks
like it is high or looks like it is low.

The ROM code waits for the signal to go high and then runs in
a loop counting how many times in a row the signal was seen as
high in order to decide if the incoming bit is a one or a zero.
The bit decision is based on the loop count for the high pulse
and hence the frequency of the tone.

The SID pin is read using the RIM machine instruction and the
results from the RIM instruction come into the A register as
the leftmost bit (that is, a zero or one goes into the leftmost
A register bit based on the level of the signal that's going
to the SID pin of the 8085 right now).

However, for this project I want to look at the SID pin by
calling the RIM instruction at regular precisely timed intervals
and instead of counting how many times it was seen as high I
want to record each time whether the bit was high or low. That
is, I want to capture one bit samples for later processing,
packing them into eight bit bytes and saving them in an array
called "samples." I think I can do this with some code that looks
"something" like this code (see below). This code SHOULD (I think)
get a "1" if the waveform coming into the cassette port is above
the zero line and should(!) get a zero if the incoming wave is
below zero.

If this actually works, and if you sent a 1200 Hz sine wave
into the cassette port audio input and if you adjusted the
timing delays for 9600 samples per second, then I would expect
eight samples for one sine wave cycle (9600 samples read per
second with 1200 cycles per second is eight samples per 1200
Hertz sine wave cycle) so the saved samples should be something
like 0xF0, 0xF0...four high/four low if sampling started at the
exact start of the sine wave, or some other byte values with
runs of four ones followed by a run of four zeros depending on
where the sine wave's phase was at the instant that sampling
started.

The delay loop timing has to be exact, and to maintain this
exact delay between samples there can only be deterministic
jumps. That is, the code can never jump based on the value of
the samples/bit coming in.  The time for the one jump per
bit can then be factored into the timing delay loop between
bits. In fact, the delay loop jumps might need to be replaced
by a run of NOPs in order to get the timing exactly correct.

OK. So... here we go (pseudocode/assembler code follows):

   ; ----------------------------------------------------
   ; setup
   ; ----------------------------------------------------
   PUSH A    ; save for later
   PUSH C    ;
   PUSH D    ;
   MOV HL, @SAMPLES ; HL points to the address for the samples[0] array

   ; ----------------------------------------------------
   ; Disable interrupts. In particular, there is a timer
   ; interrupt that goes off 256 times per second that
   ; will corrupt the precise timing that we need to
   ; perform here, so we need to disable the interrupts
   ; ----------------------------------------------------
   DI        ; disable interrupts

STARTBIT:    ; byte loop counter in C; data/bits/samples in D
   MOV C, 7  ; count of samples to be saved in one byte
   MOV D, 0  ; set D to zero for starters

   ;-----------------------------------------------------
   ; main loop for the first seven bits, timing here is
   ; the same for all bits. Just "get bit/isolate bit/save
   ; bit in register" and a delay.
   ;-----------------------------------------------------
LOOP7X:      ; get and save samples 0 to 6 (or 1 to 7, depends how you
count)
   RIM       ; get one bit sample in A register leftmost bit
   RLC       ; rotate A left thru carry to get sampled bit into carry flag
   MOV A, D  ; get samples so far from D to A
   RAL       ; rotate through carry flag again but put newest
             ; sample from carry flag into A as rightmost bit
   MOV D, A  ; save samples seen so far back to D

   ;----------------------------------------------------
   ; Busy wait loop to set the correct sampling rate
   ; to a certain number of samples per second as
   ; determined by DELAY0. Ideally we would get 9600 samples
   ; per second. Now, for the first 7 bits the delay is
   ; exactly the same because we only do "get bit/isolate
   ; bit/save bit in register." so all of these use the
   ; same DELAY0 value for this busy wait loop.
   ;----------------------------------------------------
   MOV A, DELAY0   ; timing countdown delay for bits 0-7
DELAYLOOP0:
   DCR A           ; decrement A
   JNZ DELAYLOOP0  ; are we there yet?
                   ; If A is not zero then jump back to delayloop0
   ; NOP           ; Might need a NOP or three to get the
   ;               ; timing exactly precisely correct

   ;------------------------------------------------------
   ; got seven bits yet?
   ;------------------------------------------------------
   MOV A, C   ; count of bits/samples saved so far
   DCR A      ; count = count-1
   MOV C, A
   JNZ LOOP7X ; if we don't have 7 samples yet, loop back

   ;-----------------------------------------------------
   ; Now for the eighth and final bit/sample the timing
   ; needs to be different because we need to SAVE all
   ; eight samples/bits and this takes a small fraction
   ; of a second so the delay loop time here will not
   ; be exactly the same as it was for the first seven
   ; bits.
   ;-----------------------------------------------------
GETBIT8:
   RIM        ; "get bit/isolate bit/save bit in register"
   RLC        ; save as the first secen bits
   MOV A, D
   RAL        ; A now has 8 samples
              ; don't need to save back to D here

   ;-----------------------------------------------------
   ; Save the eight bits. This takes a few cycles so the
   ; delay will need to be slightly different
   ;-----------------------------------------------------
   MOV M, A   ; save A to address in HL
   INC HL     ; increment to next element of samples array

   ;-----------------------------------------------------
   ; Similar delay loop but because we had to save the
   ; samples, the delay here will not be the same as DELAY0

; (work in progress --
; depending on whether or not we are going to get and save more samples...
; depending on whether or not we think the signal is still there...
; depending on whether or not there's still free space in samples[]...
; These will all effect DELAY8's value...
; so maybe jump back to LOOP7X or maybe we are done collecting samples
   ;-----------------------------------------------------
   MOV A, DELAY8   ; timing countdown delay for bit 8
DELAYLOOP8:
   DCR A           ; decrement A
   JNZ DELAYLOOP8  ; if a is not zero jump to delayloop8
   ; NOP           ; Might need a NOP or three to get the
   ;               ; timing exactly precisely correct

   Jxx STARTBIT    ; work in progress... maybe jump back to get more

   ; cleanup
   POP D
   POP C
   POP A
   EI         ; enable interrupts again

   ;----------------------------------------------------
   ; Next: work in progress - add digital signal processing code
   ; here to process the saved samples in the samples[] array
   ;----------------------------------------------------

-------------

I've probably made a number of silly mistakes in this whole idea.

What do you see that's wrong?

Could one bit sampling work like this?

Comments, questions, and constructive criticism are most welcome.
If you would like to be a part of this project, let me know.

Regards,
Douglas

Reply via email to