Oops... Replying to my own post to fix a couple errors, save one instruction, and adhere to the original specification more closely. ~ Bob Ammerman RAm Systems > -----Original Message----- > From: piclist-bounces@mit.edu [mailto:piclist-bounces@mit.edu] On Behalf > Of Bob Ammerman > Sent: Friday, August 11, 2017 2:19 PM > To: 'Microcontroller discussion list - Public.' > Subject: RE: [PIC] pic16f628a memory bank switching in a large circular buffer. >=20 > Here is an implementation of the OPs actual requirements, as I understand > them. One of the neat things about this implementation is that the routines > are isochronous, which means that they always take exactly the same > amount of time. >=20 > Again, untested code! >=20 > -- Bob Ammerman > RAm Systems >=20 >=20 > Again, we have the two snippets of code already defined, converted into > macros >=20 > FIXINDEX: > Convert W from a number in the range 00h..9Fh into a number in one of the > ranges 20h..6F or A0h..0EFh >=20 > BUMPINDEX > Increment W circularly, wrapping from 09Fh back to zero. >=20 > ; INSP is the insertion pointer into > ; the buffer. OFFSET is the number of > ; samples 'back' in the buffer for output >=20 > ;=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D > ; INSERTION ROUTINE: >=20 > MOVF INSP,W > BUMPINDEX > MOVWF INSP > FIXINDEX > MOVWF FSR > MOVF INBYTE,W > MOVWF INDF > RETURN >=20 > ;=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D > ; OUTPUT ROUTINE >=20 > ; Convert the offset from > ; a distance backward to > ; a distance forward >=20 > MOVWF OFFSET,W > SUBLW 0 >=20 > ; Now compute the desired location > ; in the buffer >=20 > ADDWF INSP,W >=20 > ; at this point we have to > ; wrap any value greater > ; than 09Fh. The next instruction > ; will set carry if we have a sum > ; greater then 9Fh. >=20 > ADDLW 60H >=20 > ; now we subtract 0A0h if we > ; are over 9Fh. Note that we > ; we use ADDLW instead of > ; SUBLW because the latter > ; computes 'literal minus W' > ; rather than 'W minus literal', > ; which is what we want. >=20 > BTFSC STATUS,C > ADDLW 100h-0A0h >=20 > ; Finally subtract back out > ; the 60H we added above. The > ; same comment about ADDLW vs. > ; SUBLW applies >=20 > ADDLW 100h-060h >=20 > ; Convert the value to a pointer > ; and get the byte >=20 > FIXINDEX > MOVWF FSR > MOVF INDF,W >=20 > ; Done! >=20 > RETURN --=20 http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist .