--The program acquires a 10bit TDC timestamp and computes the Fourier code encoding
--	the encoded 8bit output is in the RAM starting with RAM(1)
--  the cosine LUT is in byte3 and byte2 of each RAM location from RAM(0) to RAM(127) (i.e. 256 8bit values for the 0 to pi/4 cosine range)
--
--	config();
-- 	while(1){
--		expose();
--		t = readTDC();
--		for(int idx=1; idx<5; idx++){
--			[cosine, sine] = getTrig(idx*t);
--			RAM(2*(idx-1)) += cosine;
--			RAM(2*idx-1)+= sine;
--		}
--	}

#define processed_timestamp 0   		--register R1 is used for holding the processed timestamp
#define timestamp 1						--register R1 is used for holding the timestamp
#define cosine 2						--register R2 is used for holding the cosine of the timestamp
#define sine 3							--register R3 is used for holding the sine of the timestamp
#define R4 4				    		--register R4 is used by the LUT function
#define R5 5							--register R5 is used for miscelaneous tasks
#define CtrlOut 0b10000					--address for external trigger signal

0: SETOR 0, $0xFFFF 					--set mask for ORTree0 to allow any pixel to be used as START
1: SETTM $0b0000010011101111 			--TIME0 configured to be: TDC with internal reference, always on, will measure distance between pulse from ORTree0 and signal from CPU
2: STIMCNT								--reset input latch

3: RSTCNT								--reset TDC
4: TELL CtrlOut, 0						--strobe trigger
5: CALL 190								--expose

6: GETC timestamp, 0					--read TDC result
7: GETC processed_timestamp, 0

8: LOAD R5, $0, 1						--reset counter to 8 (for number of values in encoding)
9: LOAD R5, $16, 0	

10: CALL 30 							--find cosine and sine values of processed_timestamp

11: OR R5, R5, R5						--clear carry flag 

12: ADD (R5), cosine, (R5)				--accumulate cosine
13: OR R5, R5, R5						--clear carry flag 
14: SUBC R5, $1							--move to next component
15: ADD (R5), sine, (R5)				--accumulate sine
16: OR R5, R5, R5						--clear carry flag 
17: ADD timestamp, processed_timestamp, processed_timestamp 	--increment trig argument
18: SPLIT processed_timestamp, 1, processed_timestamp  			--only keep the lower 8 bits of the number i.e. modulo 256
19: OR R5, R5, R5						--clear carry flag 
20: SUBC R5, $1							--move to next component
21: JUMPNZ 12							--if not done with all the values, repeat 
22: JUMP 3 								--move on to next timestamp 

23: JUMP 23								--stay here


----------------------------------------------------------------------------------------------------------------------------------------------
-- LUT function; will compute the cosine and sine values of the processed_timestamp
30: LOAD R4, $0, 1						--load 128 into R4
31: LOAD R4, $128, 0
32: CMP processed_timestamp, R4
33: JUMPC 70							--the timestamp is in [0, 127]

34: LOAD R4, $256, 0					--load 256 into R4
35: CMP processed_timestamp, R4
36: JUMPC 80							--the timestamp is in [128, 255]

37: LOAD R4, $384, 0      				--load 384 into R4
38: CMP processed_timestamp, R4
39: JUMPC 90							--the timestamp is in [256, 383]

40: LOAD R4, $512, 0      				--load 512 into R4
41: CMP processed_timestamp, R4
42: JUMPC 101							--the timestamp is in [384, 511]

43: LOAD R4, $640, 0      				--load 640 into R4
44: CMP processed_timestamp, R4
45: JUMPC 112							--the timestamp is in [512, 639]

46: LOAD R4, $768, 0      				--load 768 into R4
47: CMP processed_timestamp, R4
48: JUMPC 121							--the timestamp is in [640, 767]

49: LOAD R4, $896, 0      				--load 896 into R4
50: CMP processed_timestamp, R4
51: JUMPC 130							--the timestamp is in [768, 895]

--the timestamp is in [896, 1023]
52: LOAD R4, $1023, 0 					--load the value for cosine
53: OR R5, R5, R5						--clear carry flag
54: SUB R4, processed_timestamp, cosine	--compute the index for the cosine value: 1023-x
55: LOAD R4, $896, 0 					--load the value for sine
56: OR R5, R5, R5						--clear carry flag
57: SUB processed_timestamp, R4, sine   --compute the index for the sine value: x-896 
58: FETCH (cosine), cosine, 4			--get values from LUT
59: FETCH (sine), sine, 4
60: RET 								--return

--the timestamp is in [0, 127]
70: LOAD R4, $0, 0 					    --load 0 into R4 i.e. the LUT address offset in the RAM
71: OR R5, R5, R5						--clear carry flag
72: ADD processed_timestamp, R4, cosine	--compute the index for the cosine value: x
73: LOAD R4, $127, 0 					--load 127 into R4 i.e. the value for sine
74: SUB R4, processed_timestamp, sine   --compute the index for the sine value: 127-x
75: FETCH (cosine), cosine, 3			--get values from LUT
76: FETCH (sine), sine, 3
77: NEG sine, sine						--negate sine
78: RET 								--return

--the timestamp is in [128, 255]
80: LOAD R4, $128, 0 					--load the value for cosine
81: OR R5, R5, R5						--clear carry flag
82: SUB processed_timestamp, R4, cosine	--compute the index for the cosine value: x-128
83: FETCH (cosine), cosine, 4			--get values from LUT
84: LOAD R4, $255, 0 					--load the value for sine
85: SUB R4, processed_timestamp, cosine	--compute the index for the sine value: 255-x
86: FETCH (processed_timestamp), sine, 4--get values from LUT
87: NEG sine, sine						--negate sine
88: RET 								--return

--the timestamp is in [256, 383]
90: LOAD R4, $383, 0 					--load the value for cosine
91: OR R5, R5, R5						--clear carry flag
92: SUB R4, processed_timestamp, cosine	--compute the index for the cosine value: 383-x
93: LOAD R4, $256, 0 					--load the value for sine
94: OR R5, R5, R5						--clear carry flag
95: SUB processed_timestamp, R4, sine   --compute the index for the sine value: x-256 
96: FETCH (cosine), cosine, 3			--get values from LUT
97: FETCH (sine), sine, 3
98: NEG cosine, cosine					--negate cosine
99: NEG sine, sine						--negate sine
100: RET 								--return

--the timestamp is in [384, 511]
101: LOAD R4, $511, 0 					--load the value for cosine
102: OR R5, R5, R5						--clear carry flag
103: SUB R4, processed_timestamp, cosine--compute the index for the cosine value: 511-x
104: LOAD R4, $384, 0 					--load the value for sine
105: OR R5, R5, R5						--clear carry flag
106: SUB processed_timestamp, R4, sine   --compute the index for the sine value: x-384 
107: FETCH (cosine), cosine, 4			--get values from LUT
108: FETCH (sine), sine, 4
109: NEG cosine, cosine					--negate cosine
110: NEG sine, sine						--negate sine
111: RET 								--return

--the timestamp is in [512, 639]
112: LOAD R4, $512, 0 					--load the value for cosine
113: OR R5, R5, R5						--clear carry flag
114: SUB processed_timestamp, R4, cosine--compute the index for the cosine value: x-512
115: FETCH (cosine), cosine, 3			--get values from LUT
116: LOAD R4, $639, 0 					--load the value for sine
117: SUB R4, processed_timestamp, cosine--compute the index for the sine value: 639-x
118: FETCH (processed_timestamp), sine, 3--get values from LUT
119: NEG cosine, cosine					--negate cosine
120: RET 								--return

--the timestamp is in [640, 767]
121: LOAD R4, $640, 0 					--load the value for cosine
122: OR R5, R5, R5						--clear carry flag
123: SUB processed_timestamp, R4, cosine--compute the index for the cosine value: x-640
124: FETCH (cosine), cosine, 4			--get values from LUT
125: LOAD R4, $767, 0 					--load the value for sine
126: SUB R4, processed_timestamp, cosine--compute the index for the sine value: 767-x
127: FETCH (processed_timestamp), sine, 4--get values from LUT
128: NEG cosine, cosine					--negate cosine
129: RET 								--return

--the timestamp is in [768, 895]
130: LOAD R4, $895, 0 					--load the value for cosine
131: OR R5, R5, R5						--clear carry flag
132: SUB R4, processed_timestamp, cosine--compute the index for the cosine value: 895-x
133: LOAD R4, $768, 0 					--load the value for sine
134: OR R5, R5, R5						--clear carry flag
135: SUB processed_timestamp, R4, sine  --compute the index for the sine value: x-768 
136: FETCH (cosine), cosine, 3			--get values from LUT
137: FETCH (sine), sine, 3
138: RET 								--return


----------------------------------------------------------------------------------------------------------------------------------------------
-- delay function; will stay here for 440ns 
190: LOAD R5, $0, 1						--set R5 to 24 
191: LOAD R5, $24, 0
192: SUBC R5, $1						--decrement R5
193: JUMPNZ 92							--repeat until zero
194: RET 								--return