Advertisement
dgallagher

standard_E932_E931_source

Sep 2nd, 2017
566
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;****************************************************************************************************
  2. ;* GENERAL NOTES
  3. ;* =============
  4. ;* Project started with the help of dsm-ecu Yahoo group, thanks for the great info.
  5. ;* Most disassembly comments in this file by Christian, christi999@hotmail.com.
  6. ;*
  7. ;* CPU
  8. ;* ----
  9. ;* The microcomputer chip used in the 1G DSM ECU seems to be a custom application
  10. ;* built around the 6801 architecture, Check the 6801, 6803, 6301, 68HC11 at web sites
  11. ;* such as alldatasheet.com, etc.
  12. ;*
  13. ;* CPU clock frequency is assumed to be 2MHz, i.e. the instructions cycle time is 0.5us.
  14. ;*
  15. ;* Assembly binary verifications:
  16. ;* ------------------------------
  17. ;*
  18. ;* The 2 binaries produced without any customization ("enableCustom" definition is
  19. ;* commented-out) have been verified to be identical to the E931 and E932 eprom
  20. ;* images at hand.
  21. ;*
  22. ;* To check the validity of symbolic substitution, the entire code section and tables
  23. ;* was offset by $0200 using "codeOffset" and the corresponding binary was tested on
  24. ;* my car (E932) without any problems for weeks. Additional tests were conducted by
  25. ;* writing inline code in several part of the code and no adverse effect was ever noted.
  26. ;*
  27. ;* To check the validity of symbolic substitution for ram addresses, every ram location
  28. ;* starting at $0057 was offset by 1 (i.e. temp1 was at memory address $58 instead of
  29. ;* $57, etc) and the corresponding binary was tested on my car (E932) without any problems
  30. ;* during car startup and engine revving. No additional test performed.
  31. ;*
  32. ;* This means that the code can be modified inline and in most cases, ram memories can
  33. ;* be moved around by changing the label addresses. Note however that some groups of
  34. ;* ram memories have to be moved in blocks because the code assumes they are contiguous.
  35. ;* e.g. the temp1 to temp9 variables, the inj1_offT, inj3_offT, inj4_offT and inj2_offT
  36. ;* variables, etc.
  37. ;*
  38. ;* Ram memory:
  39. ;* -----------
  40. ;* Memory from $0040 to $01bf is backed-up by battery, meaning it is preserved when the
  41. ;* ECU is powered-off as long as battery power is supplied. However, memory from $0057 to
  42. ;* $0190 is cleared to 0 by the code every time the ECU is powered-on. That can be however
  43. ;* changed by modifying the code... Battery backup was checked by disabling memory reset using
  44. ;* the "noRamReset" and then check ram memory at $018f to see if it gets preserved after power
  45. ;* off/on cycle, and it did. During the test, $018f was used as a distance counter using
  46. ;* the reed switch.
  47. ;*
  48. ;* Comments:
  49. ;* --------
  50. ;* Some comments use variable names quite loosly. For instance, multi-byte variables
  51. ;* such as [airCnt0:airCnt1:airCnt2] might be refered to as only airCnt0. airCnt0
  52. ;* might therefore refer to the single byte airCnt0, to the 16 bit value
  53. ;* [airCnt0:airCnt1] or to the 24 bit complete variable, depending on the context.
  54. ;*
  55. ;* Comments were added incrementally as my knowledge of code and variables
  56. ;* increased. As new knowledge was learned, old comments were updated or corrected
  57. ;* as much as possible but not necessarily all of them, so beware... In the end, the
  58. ;* code is the only truth... Some small areas of the code were also never completly
  59. ;* understood as a general understanding was reached and I did not care to go further
  60. ;* e.g. airflow sensor active filter reset
  61. ;*
  62. ;* Opcodes:
  63. ;* --------
  64. ;* -cmpd: cmpd1 is used for some addressing modes instead of cmpd since
  65. ;* TASM does not support unusual mitsubishi ECU cmpd opcodes..
  66. ;*
  67. ;* -brclr: branch if ALL the given bits are clear
  68. ;*
  69. ;* -brset: branch if ANY of the given bits are set (as opposed to usual
  70. ;* implementation of ALL bits set...)
  71. ;*
  72. ;* -The addressing mode using Y indexing also implicitly
  73. ;* modifies the y register. It seems that y is increased
  74. ;* by 1 or 2 depending whether the instruction is a 8 bit
  75. ;* or 16 bits operation... The following cases are confirmed
  76. ;*
  77. ;* cmpa $00,y -> y = y + 1
  78. ;* cmpb $00,y -> y = y + 1
  79. ;* ldaa $00,y -> y = y + 1
  80. ;* suba $00,y -> y = y + 1
  81. ;* ldx $00,y -> y = y + 2
  82. ;* std $00,y -> y = y + 2
  83. ;*
  84. ;*
  85. ;* Telemark assembler:
  86. ;* --------------------
  87. ;* This assembler does not provide warning messages when code assembles to
  88. ;* the same memory space, e.g. you insert code in the middle of the file
  89. ;* which result in the rest of the code to be offset by N bytes. This
  90. ;* results in the interrupt vector table to be overwritten. No warning
  91. ;* is given. The only way to know about it is to manually check the listing
  92. ;* file produced by the assembler. Check that the buffer space between
  93. ;* sections is all "$ff". Check that there is no code spilage over .org
  94. ;* statements. Check that the address space does not exceed $ffff. Use the
  95. ;* "codeOffset" at the beginnng of the file to correct the problem.
  96. ;*
  97. ;*
  98. ;* Fuel injector and coil power transistor control
  99. ;* ------------------------------------------------
  100. ;* Although the 4 fuel injectors and the 2 coil power transistors are mapped to
  101. ;* regular ports (port1, port2 and port5) which can be read to know the current
  102. ;* state of these outputs, they are also mapped in hardware to output compare
  103. ;* registers in order to activate or deactivate them at specific time instants.
  104. ;* Writing to the ports might therefore not work unless the output compare
  105. ;* configuration registers are changed to disable harware control of these
  106. ;* outputs. This might not be possible unless an "output enable" bit exists,
  107. ;* which I haven't found at this point...
  108. ;* Another way to activate or deactivate them would be to use the output
  109. ;* compare registers (as currently done by the ECU code) and provoke an
  110. ;* immediat output change.
  111. ;*
  112. ;* Here is my current understanding of how injector scheduling works, not
  113. ;* everything is clear to me so don't take this as gospel...:
  114. ;* The output compare registers for the fuel injectors seem to be at least double
  115. ;* buffered and maybe triple buffered (see schedInjSim routine). That means that
  116. ;* up to 3 different output compare values can be written to t1_outCmpWr and t2_outCmpWr
  117. ;* to activate or deactivate the injectors at those time instants. Each time a value
  118. ;* is written to t1_outCmpWr or t2_outCmpWr, the corresponding injector state
  119. ;* is also internally stored. That means that to activate injector #1 at time X,
  120. ;* you would first reset bit 0 of t1_csr, corresponding to injector #1 and then
  121. ;* write X to t1_outCmpWr. You could then immediately schedule the deactivation
  122. ;* of injector #1 by setting bit 0 of t1_csr to 1 and then write the deactivation
  123. ;* time to t1_outCmpWr. When one of the output compare register stored value matches
  124. ;* the clock at t1t2_clk, the injector is activated/deactivated and the corresponding
  125. ;* interrupt routine is called (if the interrupt mask is clear...) at outCompInt1 or
  126. ;* outCompInt2.
  127. ;*
  128. ;* Here is my current understanding of how the coil power transistor scheduling
  129. ;* works, not everything is clear to me so don't take this as gospel...: t3_outCmpWr
  130. ;* is the output compare register used to activate or deactivate the coil power
  131. ;* transistors (energize the coil and provoke ignition at the specified time instants)
  132. ;* To energize the coil for cylinder 1 and 4 at time X you would write X to t3_outCmpWr
  133. ;* and reset(0) bit 2 of t3_csr0. At time X, t3_csr0.2 would be loaded into port5.1
  134. ;* which would energize the coil. t3_csr0.2 should not be changed until that happens.
  135. ;* In the code, most of the time 2 successive values (the same one) are written to t3_outCmpWr
  136. ;* but there are some instances where only 1 value is written. My impression is that
  137. ;* the first value serves to activate/deactivate the coil power transistor at the
  138. ;* specified instant while the second one only serves to generate an interrupt
  139. ;* in order to call the outCompInt3 routine. Hence when only the coil need
  140. ;* to be activated/deactivated without calling outCompInt3, you would only write
  141. ;* one value. If in addition you want to have outCompInt3 called when the coil
  142. ;* is energized/ignited, you would write two successive values (corresponding to the
  143. ;* same time...). This is all speculation of course... As for the 2 clocks at t3_clock1
  144. ;* and t3_clock1, I assume they are connected to the same internal clock at 250KHz
  145. ;* but might be input capture registers latched when one of the two output compare
  146. ;* at t3_outCmpWr is triggered??????? Again speculation, this is the part of the code
  147. ;* I understand the least...
  148. ;*
  149. ;*
  150. ;* Timing diagram
  151. ;* --------------
  152. ;*
  153. ;* -4 cylinders = 2 rotations = 2 * 360degrees = 720 degrees
  154. ;*
  155. ;* -For sequential injection, fuel injection starts on the cas falling edge
  156. ;* i.e. cylinder #1 injection starts at -5 BTDC of #3 TDC
  157. ;*
  158. ;* -Simultaneous injection of all 4 injectors is performed when starting to
  159. ;* crank or starting a cold engine or during acceleration, check the tech manual
  160. ;* and code for more details. Simultaneous injection starts on the 5deg BTDC
  161. ;* cas signal except in the case of acceleration where it starts when an
  162. ;* injector is deactivated and no other injector is active (i.e. at the
  163. ;* beginning of the time period where no injector is active)
  164. ;*
  165. ;* -Coil energization is usually scheduled (the energization time is loaded into
  166. ;* the output compare register, energization will occur at the specified time)
  167. ;* from the cas rising edge. Coil ignition can be scheduled when energization
  168. ;* occurs (output compare interrupt) or on the cas falling edge depending on
  169. ;* the desired timing. Note however that coil energization can also be scheduled
  170. ;* when ignition occurs on the preceeding cylinder. This would correspond to
  171. ;* scheduling ignition before the cas rising edge (at high rpm I assume). Coil
  172. ;* energization can also be scheduled on the cas falling edge when the desired
  173. ;* timing is high (e.g. 10deg ATDC). As this shows, there are several combinations
  174. ;* and the complexity of the code to handle the coil reflects that fact.
  175. ;*
  176. ;*
  177. ;* No 1 TDC No 3 TDC No 4 TDC No 2 TDC
  178. ;* : : : :
  179. ;* ___________ _____
  180. ;* TDC sensor | | | |
  181. ;* signal | : | : | | : :
  182. ;* ____|___________|_______________________|_____|________________________
  183. ;* degrees 85 55 85 15
  184. ;* (BTDC/ATDC) : : : :
  185. ;* ______ ______ ______ ______
  186. ;* CAS sensor | | | | | | | |
  187. ;* signal | | : | | : | | : | | :
  188. ;* _____|______|__________|______|__________|______|__________|______|____
  189. ;* degrees 75 5 : 75 5 : 75 5 : 75 5 :
  190. ;* (BTDC) : : : :
  191. ;*
  192. ;* : : : :
  193. ;* No 1 cyl. compression : combustion : exhaust : intake : compression
  194. ;* No 3 cyl. intake : compression : combustion : exhaust : intake
  195. ;* No 4 cyl. exhaust : intake : compression : combustion : exhaust
  196. ;* No 2 cyl. combustion : exhaust : intake : compression : combustion
  197. ;*
  198. ;*
  199. ;*
  200. ;* Airflow calculations dependencies, more details in code
  201. ;* --------------------------------------------------------
  202. ;*
  203. ;* masProc: airflow sensor interrupt, increases [airCntNew0:airCntNew1]
  204. ;* | by airQuantum for every airflow sensor pulse received
  205. ;* |
  206. ;* |
  207. ;* |
  208. ;* |--> [airCntNew0:airCntNew1]: Increased by airQuantum for every airflow sensor pulse
  209. ;* | Reset and used as input to [airCnt0:airCnt1:airCnt2]
  210. ;* | on every cas falling edge, i.e. air is counted twice
  211. ;* | per rotation, once for every cylinder cycle... It can
  212. ;* | therefore be seen as the air count per cylinder.
  213. ;* |
  214. ;* |--> [airCnt0:airCnt1:airCnt2]: Filtered version of 256*[airCntNew0:airCntNew1]
  215. ;* | exponential averaging is used.
  216. ;* |
  217. ;* |
  218. ;* |
  219. ;* |--> mafraw16: 16 bit airflow sensor pulse frequency (mafraw16/10.24)Hz
  220. ;* | | mafraw16 = 8205*[airCnt0:airCnt1]/Tcas
  221. ;* | |
  222. ;* | |
  223. ;* | |--> mafraw: 8 bit airflow sensor pulse frequency (6.25*mafraw)Hz
  224. ;* | mafraw: = mafraw16/64
  225. ;* |
  226. ;* |
  227. ;* |
  228. ;* |--> airVol16: Equals [airCnt0:airCnt1] * masScalar/65536
  229. ;* | |
  230. ;* | |
  231. ;* | |
  232. ;* | |--> airVol : Equals airVol16/2
  233. ;* | |--> airVolT : Equals airVol16/2 * iatCompFact/128
  234. ;* | |--> airVolTB : Equals airVol16/2 * iatCompFact/128 * baroFact/128
  235. ;* | |--> airVolB : Equals airVol16/2 * baroFact/128
  236. ;* |
  237. ;* |
  238. ;* |--> injPw: Injector pulse width in "normal" operation,
  239. ;* injPw = [airCnt0:airCnt1] * injFactor/256 + other corrections
  240. ;*
  241. ;*
  242. ;*
  243. ;* Discussion on MAS compensation factors
  244. ;* ---------------------------------------
  245. ;*
  246. ;* Total airflow sensor compensation is made-up of:
  247. ;*
  248. ;* totMasComp(freq,iat,baro) = masComp + t_masComp(freq) + t_masLin(freq,iat,baro)
  249. ;*
  250. ;* where maxComp is a fixed offset ($64 for 1G and $40 for 2G) and t_masComp and t_masLin
  251. ;* are table values interpolated from frequency, intake air temperature and barometric
  252. ;* pressure. t_masComp(freq) is basically compensation for the airflow sensor charcteristic
  253. ;* curve as a function of frequency (to linearize the number of pulse per sec vs. the volume
  254. ;* of air passing through the sensor) while t_masLin(freq,iat,baro) is a smaller factor
  255. ;* probably compensating for temperature drift (electronic) and airflow characteristic
  256. ;* change as a function of air density???
  257. ;*
  258. ;* Assuming the following:
  259. ;*
  260. ;* -injComp = 100% (for 260cc injectors at 36psi)
  261. ;* -workFtrim = 100%
  262. ;* -o2FuelAdj = 100%
  263. ;* -iatCompFact = 100% (at 25.6degC)
  264. ;* -baroFact = 100% (~1 bar)
  265. ;* -openLoopEnr = 100%
  266. ;* -coldTempEnr = 100%
  267. ;* -enrWarmup = 0%
  268. ;*
  269. ;*
  270. ;* Then the injector pulswidth is calculated by the ECU as (excluding deadtime)
  271. ;*
  272. ;* injPw(usec/cylinder) = numPulsePerCasInterrupts *$9c * totMasComp * 16/256
  273. ;* = numPulsePerCasInterrupts * totMasComp * 9.75
  274. ;*
  275. ;* If we also assume a 14.7 air to fuel ratio, Dair=1.18 air density (g/litre) at 25degC,
  276. ;* Dgas=0.775 fuel density (g/cc) then we would need 23900 usec of injection per
  277. ;* litre of air using the same 260cc at 36psi, working that factor into the equation, we
  278. ;* get
  279. ;*
  280. ;* injPw(usec/cylinder) = numPulsePerCasInterrupts * totMasComp * 9.75
  281. ;* = numPulsePerCasInterrupts * totMasComp/2452 * 2452 * 9.75
  282. ;* = numPulsePerCasInterrupts * totMasComp/2452 * 23900usecOfInjection/litreOfAir
  283. ;*
  284. ;* This means that under the above assumptions, totMasComp/2452 has units of
  285. ;* litreOfAirPerAirflowSensorPulse.
  286. ;*
  287. ;* The factor 2452 is similar to the one provided by J. Oberholtzer, I think.
  288. ;* The exact value must be somewhere in that range...
  289. ;*
  290. ;* masScalar is also used for maf compensation ($5e86,24198 for 1G, $7A03,31235 for 2g)
  291. ;* for controls other than fuel injection. It probably correspond to some metric of
  292. ;* the totMasComp curve (average or max under given conditions). From 1G and 2G numbers,
  293. ;* It could correspond to the max of the masComp + t_masComp(freq) curve multiplied
  294. ;* by 0.808*128? It could also correspond to the masComp + t_masComp(freq) curve
  295. ;* sampled at around 69Hz and multiplied by 128.
  296. ;*
  297. ;* masScalar = maxTotMasComp*0.808*128 = totMasComp(69Hz)*128
  298. ;*
  299. ;* We then have in the case of masScalar = maxTotMasComp*0.808*128:
  300. ;*
  301. ;* airVol16 = numPulsePerCasInterrupts * $9c * masScalar / 65536
  302. ;* = numPulsePerCasInterrupts * $9c * maxTotMasComp*0.808*128 / 65536
  303. ;* = numPulsePerCasInterrupts * maxTotMasComp * 0.2462
  304. ;* = numPulsePerCasInterrupts * maxTotMasComp/2452 * 2452*0.2462
  305. ;* = numPulsePerCasInterrupts * maxTotMasComp/2452 * 603.68
  306. ;*
  307. ;* since totMasComp/2452 is litreOfAirPerAirflowSensorPulse, we have
  308. ;*
  309. ;* airVol16 = numPulsePerCasInterrupts * litreOfAirPerAirflowSensorPulse * 603.68
  310. ;*
  311. ;* Using again 1.18g/litre air density we get
  312. ;*
  313. ;* airVol16 = numPulsePerCasInterrupts * litreOfAirPerAirflowSensorPulse *1.18 * 603.68/1.18
  314. ;* = numPulsePerCasInterrupts * gramsOfAirPerAirflowSensorPulse * 512
  315. ;* = gramsOfAirPerCasInterrupts * 512
  316. ;*
  317. ;* In that case, airVol16/512 can be seen has having units of gramsOfAirPerCasInterrupts
  318. ;* (grams of air entering one cylinder). Note that the factor of 512 is not random, the
  319. ;* factor 0.808 is used to get it in that case...
  320. ;*
  321. ;* The load index values used to interpolate the fuel map is then
  322. ;*
  323. ;* airVol16/2 <= 96
  324. ;*
  325. ;* loadIndex = (airVol16/2-32)/16
  326. ;* = (gramsOfAirPerCasInterrupts*512/2 -32)/16
  327. ;* = gramsOfAirPerCasInterrupts*16-2
  328. ;*
  329. ;* airVol16/2 >= 96
  330. ;*
  331. ;* loadIndex = gramsOfAirPerCasInterrupts * 512/2 * 0.668/16
  332. ;* = gramsOfAirPerCasInterrupts*10.69
  333. ;*
  334. ;* Which correspond to (gramsOfAirPerCasInterrupts for each index value)
  335. ;*
  336. ;* 0 1 2 3 4 5 6 7 8 9 10 11
  337. ;* 0.125 0.1875 0.25 0.3125 0.3750 0.4678 0.5614 0.6549 0.7485 0.8421 0.9356 1.0292
  338. ;*
  339. ;* gramsOfAirPerRevolution would be twice those values. Notice that the max value of 1.0292
  340. ;* correspond to about 250HP when BSFC=0.55 which is in the range of the stock 1G 195HP...
  341. ;*
  342. ;* Also notice that the 8 bit airflow airVol = airVol16/2 will saturate to $ff when
  343. ;* airVol16/2 = 255 which correspond to gramsOfAirPerCasInterrupts = 1 gram. airVolT
  344. ;* airVolTB and airVolB will also saturate in the same range...
  345. ;*
  346. ;* We can now compare these results with the stock boost gauge. It has a max range
  347. ;* of 1Kg per sq cm which equals 14.2 psi. The boost gauge duty cycle is given by
  348. ;*
  349. ;* bGaugeODuty = t_bGauge(airVolT/32)/24
  350. ;*
  351. ;* When maximum airVolT = 255 = iatCompFact*airVol16/2, bGaugeODuty = 20/24 = 0.83.
  352. ;* At 25.6 degC, iatCompFact = 1.0 and therefore airVol16=510 which translates to
  353. ;* 1g of air. boost gauge duty of 0.83 correspond to approx. 10.9psi (by eye...).
  354. ;* Assuming a displacement of 0.5litre per cylinder and charge air density of 1.18
  355. ;* (25degC, probably too low for that psi range, unless you have a perfect intercooler..)
  356. ;* we would get 1.18*0.5*(10.9+14.5)/14.5 = 1.03g of air per cylinder (cas
  357. ;* interrupt). This is quite close to the 1.0g we had earlier.
  358. ;*
  359. ;* The 0psi point on the gauge correspond to a duty cycle of about 40.5% which
  360. ;* correspond to bGaugeODuty=9.75/24 which from t_bGauge correspond to
  361. ;* airVolT/32=2.875 which means airVolT = 92. with iatCompFact = 1.0 @25degC,
  362. ;* we get airVol16 = 2*airVolT/iatCompFact = 184 which correspond to 0.36grams of air
  363. ;* Assuming a displacement of 0.5litre per cylinder and charge air density of 1.18@25degC
  364. ;* we would get 1.18*0.5 = 0.59g of air per cylinder (cas interrupt) at 0psi. Compared to
  365. ;* 0.36g we had earlier this is a large error but then there are several factor not taken onto
  366. ;* account in the calculations, I suppose???.
  367. ;*
  368. ;*
  369. ;* Engine coolant and intake air temperature
  370. ;* ------------------------------------------
  371. ;*
  372. ;* Approximate sensor curves (temperature
  373. ;* against ADC value, taken from MMCD). The
  374. ;* control points in the service manual are
  375. ;* quite close (0 to 2 degC off).
  376. ;*
  377. ;*
  378. ;* ADC ECT IAT ADC ECT IAT ADC ECT IAT ADC ECT IAT
  379. ;* degC degC degC degC
  380. ;*
  381. ;* $00 158.0 184.0 $40 52.0 56.0 $80 21.0 23.0 $c0 -7.0 -7.0
  382. ;* $01 154.4 178.1 $41 51.3 55.3 $81 20.6 22.5 $c1 -7.5 -7.6
  383. ;* $02 150.9 172.5 $42 50.7 54.6 $82 20.2 22.1 $c2 -8.1 -8.2
  384. ;* $03 147.5 167.2 $43 50.1 53.9 $83 19.8 21.7 $c3 -8.6 -8.8
  385. ;* $04 144.2 162.0 $44 49.5 53.3 $84 19.4 21.2 $c4 -9.2 -9.4
  386. ;* $05 140.9 157.1 $45 48.9 52.6 $85 19.0 20.8 $c5 -9.8 -10.1
  387. ;* $06 137.7 152.4 $46 48.3 52.0 $86 18.7 20.4 $c6 -10.4 -10.7
  388. ;* $07 134.6 148.0 $47 47.7 51.3 $87 18.3 19.9 $c7 -10.9 -11.3
  389. ;* $08 131.6 143.7 $48 47.2 50.7 $88 17.9 19.5 $c8 -11.5 -12.0
  390. ;* $09 128.6 139.6 $49 46.6 50.1 $89 17.6 19.0 $c9 -12.1 -12.6
  391. ;* $0a 125.7 135.7 $4a 46.1 49.4 $8a 17.2 18.6 $ca -12.7 -13.2
  392. ;* $0b 122.9 132.0 $4b 45.6 48.8 $8b 16.9 18.2 $cb -13.2 -13.9
  393. ;* $0c 120.2 128.5 $4c 45.0 48.2 $8c 16.5 17.7 $cc -13.8 -14.5
  394. ;* $0d 117.5 125.1 $4d 44.5 47.7 $8d 16.1 17.3 $cd -14.3 -15.1
  395. ;* $0e 114.9 121.9 $4e 44.0 47.1 $8e 15.7 16.8 $ce -14.9 -15.7
  396. ;* $0f 112.4 118.8 $4f 43.5 46.5 $8f 15.3 16.4 $cf -15.4 -16.3
  397. ;* $10 110.0 116.0 $50 43.0 46.0 $90 15.0 16.0 $d0 -16.0 -17.0
  398. ;* $11 107.6 113.2 $51 42.4 45.4 $91 14.5 15.5 $d1 -16.5 -17.6
  399. ;* $12 105.3 110.6 $52 41.9 44.9 $92 14.1 15.1 $d2 -17.0 -18.2
  400. ;* $13 103.0 108.1 $53 41.4 44.3 $93 13.7 14.6 $d3 -17.5 -18.8
  401. ;* $14 100.8 105.8 $54 40.9 43.8 $94 13.3 14.2 $d4 -18.0 -19.4
  402. ;* $15 98.7 103.5 $55 40.4 43.3 $95 12.9 13.7 $d5 -18.6 -20.1
  403. ;* $16 96.7 101.4 $56 39.9 42.8 $96 12.4 13.3 $d6 -19.2 -20.8
  404. ;* $17 94.7 99.4 $57 39.3 42.3 $97 12.0 12.8 $d7 -19.8 -21.5
  405. ;* $18 92.8 97.5 $58 38.8 41.8 $98 11.5 12.4 $d8 -20.5 -22.3
  406. ;* $19 91.0 95.7 $59 38.3 41.4 $99 11.1 12.0 $d9 -21.3 -23.1
  407. ;* $1a 89.2 93.9 $5a 37.8 40.9 $9a 10.6 11.5 $da -22.1 -24.0
  408. ;* $1b 87.5 92.3 $5b 37.3 40.4 $9b 10.2 11.1 $db -23.0 -24.9
  409. ;* $1c 85.9 90.7 $5c 36.9 39.9 $9c 9.7 10.7 $dc -24.0 -26.0
  410. ;* $1d 84.3 89.2 $5d 36.4 39.4 $9d 9.3 10.2 $dd -25.0 -27.1
  411. ;* $1e 82.8 87.7 $5e 35.9 38.9 $9e 8.8 9.8 $de -26.2 -28.3
  412. ;* $1f 81.3 86.3 $5f 35.4 38.4 $9f 8.4 9.4 $df -27.5 -29.6
  413. ;* $20 80.0 85.0 $60 35.0 38.0 $a0 8.0 9.0 $e0 -29.0 -31.0
  414. ;* $21 78.6 83.6 $61 34.5 37.5 $a1 7.5 8.5 $e1 -30.5 -32.5
  415. ;* $22 77.4 82.4 $62 34.0 37.0 $a2 7.1 8.1 $e2 -32.2 -34.1
  416. ;* $23 76.2 81.1 $63 33.6 36.4 $a3 6.6 7.7 $e3 -33.9 -35.7
  417. ;* $24 75.0 79.9 $64 33.1 35.9 $a4 6.2 7.3 $e4 -35.8 -37.5
  418. ;* $25 73.9 78.8 $65 32.7 35.4 $a5 5.8 6.9 $e5 -37.7 -39.3
  419. ;* $26 72.9 77.7 $66 32.3 34.9 $a6 5.3 6.4 $e6 -39.7 -41.2
  420. ;* $27 71.9 76.6 $67 31.8 34.4 $a7 4.9 6.0 $e7 -41.7 -43.0
  421. ;* $28 70.9 75.5 $68 31.4 33.9 $a8 4.5 5.6 $e8 -43.7 -44.9
  422. ;* $29 69.9 74.5 $69 31.0 33.4 $a9 4.0 5.2 $e9 -45.8 -46.8
  423. ;* $2a 69.0 73.5 $6a 30.5 32.9 $aa 3.6 4.7 $ea -47.8 -48.7
  424. ;* $2b 68.1 72.5 $6b 30.1 32.4 $ab 3.2 4.3 $eb -49.8 -50.6
  425. ;* $2c 67.3 71.5 $6c 29.7 31.9 $ac 2.7 3.8 $ec -51.8 -52.4
  426. ;* $2d 66.4 70.6 $6d 29.3 31.4 $ad 2.3 3.4 $ed -53.7 -54.1
  427. ;* $2e 65.6 69.7 $6e 28.8 30.9 $ae 1.8 2.9 $ee -55.5 -55.8
  428. ;* $2f 64.8 68.8 $6f 28.4 30.4 $af 1.4 2.4 $ef -57.3 -57.4
  429. ;* $30 64.0 68.0 $70 28.0 30.0 $b0 1.0 2.0 $f0 -59.0 -59.0
  430. ;* $31 63.1 67.1 $71 27.5 29.5 $b1 0.5 1.5 $f1 -59.0 -59.0
  431. ;* $32 62.3 66.3 $72 27.1 29.0 $b2 0.0 0.9 $f2 -59.0 -59.0
  432. ;* $33 61.5 65.5 $73 26.6 28.6 $b3 -0.3 0.4 $f3 -59.0 -59.0
  433. ;* $34 60.7 64.7 $74 26.2 28.1 $b4 -0.8 -0.0 $f4 -59.0 -59.0
  434. ;* $35 59.9 63.9 $75 25.7 27.7 $b5 -1.3 -0.5 $f5 -59.0 -59.0
  435. ;* $36 59.2 63.1 $76 25.3 27.2 $b6 -1.8 -1.1 $f6 -59.0 -59.0
  436. ;* $37 58.4 62.3 $77 24.8 26.8 $b7 -2.3 -1.6 $f7 -59.0 -59.0
  437. ;* $38 57.6 61.6 $78 24.4 26.4 $b8 -2.8 -2.2 $f8 -59.0 -59.0
  438. ;* $39 56.9 60.9 $79 23.9 25.9 $b9 -3.3 -2.8 $f9 -59.0 -59.0
  439. ;* $3a 56.1 60.1 $7a 23.5 25.5 $ba -3.8 -3.3 $fa -59.0 -59.0
  440. ;* $3b 55.4 59.4 $7b 23.0 25.1 $bb -4.3 -3.9 $fb -59.0 -59.0
  441. ;* $3c 54.7 58.7 $7c 22.6 24.7 $bc -4.8 -4.5 $fc -59.0 -59.0
  442. ;* $3d 54.0 58.0 $7d 22.2 24.2 $bd -5.3 -5.1 $fd -59.0 -59.0
  443. ;* $3e 53.3 57.3 $7e 21.8 23.8 $be -5.9 -5.7 $fe -59.0 -59.0
  444. ;* $3f 52.6 56.6 $7f 21.4 23.4 $bf -6.4 -6.3 $ff -59.0 -59.0
  445. ;*
  446. ;*
  447. ;*
  448. ;*
  449. ;****************************************************************************************************
  450.  
  451. ;***************************************************************
  452. ;*
  453. ;*
  454. ;* Assembler general settings
  455. ;*
  456. ;*
  457. ;*
  458. ;***************************************************************
  459. .msfirst ; Assembler endian setting, do not change
  460. .define E931 ; E931 or E932 depending on desired output
  461. ;.define enableCustom ; Define to enable custom features below, comment-out to get the original E931 or E932 binaries
  462.  
  463. #ifdef enableCustom
  464. ;-----------------
  465. ; Custom settings
  466. ;-----------------
  467. codeOffset .equ $0100 ; Allows to move all the code up in the eprom to make space for new code, Original offset is 0.
  468. .define ftrimMax $b3 ; Maximum fuel trim adjustement (xx/$80)%, $b3=140%
  469. .define fuelMapClip $d0 ; Fuel map max value (will be clippped to this in code)
  470. .define injComp $31 ; Injector size compensation referenced to $80=100% for 260cc at 36psi: 390cc(4E,43psi);450(4A);510(41);550(3D);600(38);650(33);660(32);680(31);700(30);750(2C);800(2A);850(27);
  471. .define idleVal $64 ; Idle speed /8, Normal $60
  472. .define idleDrVal $57 ; Idle speed /8, Normal $53
  473. .define fuelCutVal $ff ; Fuel cut value, Original $a0
  474. .define masComp $40 ; Mas multiplier (1G:$64, 2G:$40)
  475. .define masScalar $7a03 ; Mas scalar (1G:$5e86, 2G:$7a03)
  476. .define baudRate $02 ; BaudRate divider->00(125000baud),01(15625baud),02(1953baud),03(488baud)
  477.  
  478. .define custDeadTime ; Use custom injector deadtime table
  479. .define custMas ; Use custom MAS table
  480. .define custFuelMap ; Use custom fuel map
  481. .define custTimingMap ; Use custom timing map
  482. .define custOctaneMap ; Use custom octane map
  483. .define octaneReset ; Reset octane on every start
  484. .define extLoadRange ; Extended load range for timing, fuel and octane maps...
  485. .define extLoadRange2 ; Use temperature compensation for load calc when extLoadRange is enabled
  486. .define batteryGauge ; Battery gauge instead of boost gauge
  487. .define masLog2X ; Double the MAS logging range
  488. ;.define noFuelCut ; Remove fuel cut altogether
  489. ;.define noRamReset ;
  490. ;.define noClosedLoop ; Remove closed loop mode, for testing...
  491.  
  492. #else
  493. #ifdef E931
  494. ;--------------------------------------
  495. ; Default values for original 931 ECU
  496. ;--------------------------------------
  497. codeOffset .equ $0000 ;
  498. .define ftrimMax $b3 ;
  499. .define fuelMapClip $ca ;
  500. .define injComp $4a ; 450cc injectors used at 36psi...
  501. .define idleVal $60 ;
  502. .define fuelCutVal $a0 ;
  503. .define masComp $64 ;
  504. .define masScalar $5e86 ;
  505. .define baudRate $02 ;
  506. #else
  507. ;--------------------------------------
  508. ; Default values for original 932 ECU
  509. ;--------------------------------------
  510. codeOffset .equ $0000 ;
  511. .define ftrimMax $b0 ;
  512. .define fuelMapClip $c0 ;
  513. .define injComp $4e ; 390cc injectors used at 43psi, the value reflects that pressure difference compared to E931
  514. .define idleVal $60 ;
  515. .define idleDrVal $53 ;
  516. .define fuelCutVal $a0 ;
  517. .define masComp $64 ;
  518. .define masScalar $5e86 ;
  519. .define baudRate $02 ;
  520. #endif
  521. #endif
  522.  
  523.  
  524.  
  525. ;***************************************************************
  526. ;*
  527. ;*
  528. ;* Microcontroller registers
  529. ;*
  530. ;*
  531. ;***************************************************************
  532. p1_ddr .EQU $0000 ; Port 1 data direction register. Initialized with $7E=01111110 (0=intput, 1=output)
  533. p2_ddr .EQU $0001 ; Port 2 data direction register. Initialized with $16=00010110
  534. port1 .EQU $0002 ; Port 1 Data register
  535. ; bit 0 (0x01): in - Unused but varies(seems to have correlation with CAS), by extrapolation, set to out for injector #5 or #6 on other ECUs?
  536. ; bit 1 (0x02): out - Set to 0 to activate injector #3?
  537. ; bit 2 (0x04): out - Set to 0 to activate injector #2?
  538. ; bit 3 (0x08): out - Set to 0 to activate injector #4?
  539. ; bit 4 (0x10): out - Fuel pump relay
  540. ; bit 5 (0x20): out - Air cond. clutch
  541. ; bit 6 (0x40): out - ???, reset to 0 on init and first sub
  542. ; bit 7 (0x80): in - Reed switch, 4 square pulse (square wave) per odometer rotation, each of the 4 complete square wave correspond to ~40cm (20cm for each rising or falling edge)
  543. port2 .EQU $0003 ; Port 2 Data register
  544. ; bit 0 (0x01): in - Unused but varies (seems to have correlation with CAS), by extrapolation, set to out for injector #5 or #6 on other ECUs?
  545. ; bit 1 (0x02): out - Set to 0 to activate injector #1?
  546. ; bit 2 (0x04): out - Airflow sensor active filter reset. Set/reset depending on tps,rpm,airVol,idleSwitch?????? (in serial clock)- Connected to serial port clock???
  547. ; bit 3 (0x08): in - Connected to serial port input (if serial RE is enabled) and test connector serial interface
  548. ; bit 4 (0x10): out - Connected to serial port output (if serial TE is enabled) and test connector serial interface, controlled directly to output heart beat code to test connector
  549. ; bit 5 (0x20): in - 0, ECU Operating mode PC0 (latched on ECU reset)
  550. ; bit 6 (0x40): in - 1, ECU Operating mode PC1
  551. ; bit 7 (0x80): in - 0, ECU Operating mode PC2
  552. p3_ddr .EQU $0004 ; Port 3 data direction register, Initialized to 0 (all input)
  553. p4_ddr .EQU $0005 ; Port 4 data direction register, Initialized to 0 (all input)
  554. port3 .EQU $0006 ; Port 3 Data register
  555. ; bit 0 (0x01): in - IG2 related, 0 when IG2 at +12V??? (ABS unit?????) see around Md4d4 and M23db?
  556. ; bit 1 (0x02): in - IG1. 0 when IG1 at +12V. Set to 1 when power has been turned off and control relay is about to turn off. i.e. ECU is going to loose power in a short while.
  557. ; bit 2 (0x04): in - Top dead center sensor signal (TDC). Set to 0 when TDC signal is active
  558. ; bit 3 (0x08): in - Set to 1 if power steering pump is activated
  559. ; bit 4 (0x10): in - Air cond. switch (1=off). 0 indicate that AC should be activated, if possible... Connected to the output of the A/C control unit through the the ECT switch (switch cuts signal therefore asking ECU to cut clutch...)
  560. ; bit 5 (0x20): in - Inhibitor switch (A/T only) Set to 1 when transmission is in park or neutral
  561. ; bit 6 (0x40): in - 0 if key is in start position
  562. ; bit 7 (0x80): in - Set to 1 when the idle switch is on
  563. port4 .EQU $0007 ; Port 4 data register
  564. ; bit 0 (0x01): in - c0, set when config resistor R129 is installed. used in conjucntion with c1 in #t_strap1 lookup, FEDERAL (0) or CALIFORNIA (1)
  565. ; bit 1 (0x02): in - c1, set when config resistor R130 is installed. used in conjucntion with c0 in #t_strap1 lookup, FWD (0) or AWD (1)
  566. ; bit 2 (0x04): in - Signal from the ignition sensing circuit. Toggled on every ignition signal sent to the coil (toggled on every cylinder ignition if the power transistor output changed...), stays at the given level from one ignition to the other
  567. ; bit 3 (0x08): in - Set to 1 when ECU test mode terminal is grounded
  568. ; bit 4 (0x10): in - Set to 1 when the timing terminal is grounded
  569. ; bit 5 (0x20): in - Knock sensor feedback? (set to 1 indicates it works...)???
  570. ; bit 6 (0x40): in - Fuel pump driven feedback? 0 when FP is driven?
  571. ; bit 7 (0x80): in - Injector driven feedback. Set to 0 when injector circuit is working properly??? Bit is tested when an injector to test was just deactivated and no other injector is active??? Bit might be loaded on the falling edge of the injector driving current???
  572. ; Service manual says injector is bad if injector is not continuously driven for 4 sec during idle or cranking. 4 sec is implemented by fault code regular code... So this bit would be "injector driven" bit
  573. t1_csr .EQU $0008 ; Dual of $18, Timer1 control and status register, dual of t2_csr
  574. ; bit 0 (0x01): Injector 1 activation/deactivation bit. Bit is transfered to port2.1 when a t1 or t2 output compare interrupt is generated???
  575. ; bit 1 (0x02): cas edge detection polarity, set to 0 to trigger an interrupt on the CAS rising edge, set to 1 to trigger an interrupt on the CAS falling edge
  576. ; bit 2 (0x04): By extrapolation, set to 0 when injector 5/6 is on????
  577. ; bit 3 (0x08): Set to 1 to enable outCompInt1 interrupts (injector 1 only or 1 and 4)?
  578. ; bit 4 (0x10): Set to 1 to enable inCaptInt1 interrupts (cas)?
  579. ; bit 5 (0x20): By extrapolation, set to 0 when injector 5/6 is on????
  580. ; bit 6 (0x40): 1 indicate that outCompInt1 interrupt is pending/has been activated (injector #1 or #4 activation/deactivation)
  581. ; bit 7 (0x80): 1 indicate that inCaptInt1 interrupt is pending/has been activated (cas)
  582. t1t2_clk .EQU $0009 ;:$000a ; Free running counter at 1MHz for t1 and t2 timer functions
  583. t1_outCmpWr .EQU $000b ;:$000c ; Dual of $001B, Output compare register, value is compared to t1t2_clk and when a match occurs, injector ports are loaded with the values indicated in t1_csr. Seems 2 or 3 successive value can be written (injector activation and deactivation times...)
  584. t1_inCapt .EQU $000d ;:$000e ; Cas sensor input capture register. Contains the value of t1t2_clk when the cas sensor "edge" was detected
  585. L000f .EQU $000f ; Init to 0??????????????
  586. sci_baud .EQU $0010 ; Serial communication rate and mode control register (clock source = 2MHz)
  587. ; bit 0 (0x01): SS0, [SS1:SS0] is baud rate divider, 00(16) 01(128) 10(1024) 11(4096), assuming basic clock of 2MHZ, we get 125000baud, 15625baud, 1953baud, 488baud
  588. ; bit 1 (0x02): SS1
  589. ; bit 2 (0x04): CC0, [CC1:CC0] is the mode control register
  590. ; bit 3 (0x08): CC1
  591. ; bit 4 (0x10): NU?
  592. ; bit 5 (0x20): NU?
  593. ; bit 6 (0x40): NU?
  594. ; bit 7 (0x80): NU?
  595. sci_scr .EQU $0011 ; Serial communication status and control register?
  596. ; bit 0 (0x01): WU - Wake-up on idle line
  597. ; bit 1 (0x02): TE - transmit enable, set to 1
  598. ; bit 2 (0x04): TIE - Tx interrupt enable, reset to 0
  599. ; bit 3 (0x08): RE - Rx enable, checked for set before tx
  600. ; bit 4 (0x10): RIE - Rx interrupt enable, Reset/set to 0/1 in real time int
  601. ; bit 5 (0x20): TDRE - transmit data register empty
  602. ; bit 6 (0x40) ORFE - Overrun and framing error
  603. ; bit 7 (0x80): RDRF - Read data register full
  604. sci_rx .EQU $0012 ; SCI data read register
  605. sci_tx .EQU $0013 ; SCI data write register
  606. ramControl .EQU $0014 ; RAM control register/battery saving status register
  607. ; bit 0 (0x01): Init to 0?
  608. ; bit 1 (0x02): Init to 0?
  609. ; bit 2 (0x04): Init to 0?
  610. ; bit 3 (0x08): Init to 0?
  611. ; bit 4 (0x10): Init to 0?
  612. ; bit 5 (0x20): Init to 0?
  613. ; bit 6 (0x40): Ram enable bit??? Set to 1 after the fresh reset initialization is done, reset to 0 in failureInt?
  614. ; bit 7 (0x80): Power standby bit, Set to 1 after the fresh reset initialization is done, reset to 0 if we loose standby power (i.e. 0 when ram content was not preserved after a power-off)
  615. p5_ddr .EQU $0015 ; Port 5 data direction register, Initialized to $#fe (1111 1110)
  616. port5 .EQU $0016 ; Port 5
  617. ; bit 0 (0x01): in - CAS, crank angle sensor signal. Set to 0 when the CAS signal is activated
  618. ; bit 1 (0x02): out - Power transistor output for cyl 1 and 4. Set to 0 to energize the coil.
  619. ; bit 2 (0x04): out - Power transistor output for cyl 2 and 3. Set to 0 to energize the coil.
  620. ; bit 3 (0x08): out - EGR control solenoid output
  621. ; bit 4 (0x10): out - Fuel pressure solenoid output (0=activated)
  622. ; bit 5 (0x20): out - Boost control solenoid output
  623. ; bit 6 (0x40): out - ISC step control, see table t_iscPattern
  624. ; bit 7 (0x80): out - ISC step control, see table t_iscPattern
  625. L0017 .EQU $0017 ; Init to 0?????
  626. t2_csr .EQU $0018 ; Timer2 control and status register, uses the same clock as timer 1 (t1t2_clk)
  627. ; bit 0 (0x01): Injector 3 activation/deactivation bit. Bit is transfered to port1.1 when a t1 or t2 output compare interrupt is generated
  628. ; bit 1 (0x02): Airflow sensor edge detection polarity (0=rising edge, 1=falling edge, or the opposite?). See masProc subroutine header
  629. ; bit 2 (0x04): Injector 2 activation/deactivation bit. Bit is transfered to port1.2 when a t1 or t2 output compare interrupt is generated
  630. ; bit 3 (0x08): Set to 1 to enable outCompInt2 interrupts (injectors 2, 3 and maybe 4)?
  631. ; bit 4 (0x10): Set to 1 to enable inCaptInt2 interrupts (airflow sensor)?
  632. ; bit 5 (0x20): Injector 4 activation/deactivation bit. Bit is transfered to port1.3 when a t1 or t2 output compare interrupt is generated
  633. ; bit 6 (0x40): 1 indicate that outCompInt2 interrupt is pending/has been activated (injectors #2 or #3 activation/deactivation)
  634. ; bit 7 (0x80): 1 indicate that inCaptInt2 interruot is pending/has been activated (airflow sensor pulse)
  635. t3_csr0 .EQU $0019 ; Normally the dual of $0009 but since the ECU didn't need the equivalent of t1t2_clk for timer 2 (timer 1 and timer 2 both use t1t2_clk), it is used for something else...
  636. ; timer 3 (coil) control ans status register 0 ???
  637. ; bit 0 (0x01): 0 all the time except, set to 1 when no cas interrupt received for 1.275sec???
  638. ; bit 1 (0x02): 1 on every loop
  639. ; bit 2 (0x04): 1 Set to 0 when the output compare interrupt need to energize coil for cylinder 1 or 4, i.e. bit will be loaded in port5.1 when interrupt occur
  640. ; bit 3 (0x08): 1 Set to 0 when the output compare interrupt need to energize coil for cylinder 2 or 3, i.e. bit will be loaded in port5.2 when interrupt occur
  641. ; bit 4 (0x10): 1 on init but not on every loop, Used to decide which of t3_clock1 or t3_clock2 should be used upon a CAS interrupt???
  642. ; bit 5 (0x20): 0 on every loop
  643. ; bit 6 (0x40): 1 on every loop
  644. ; bit 7 (0x80): 0 on every loop
  645. t3_csr1 .EQU $001a ; Normally the dual of $000a but since the ECU didn't need the equivalent of t1t2_clk for timer 2 (timer 1 and timer 2 both use t1t2_clk), it is used for something else...
  646. ; timer 3 (coil) control ans status register 1???
  647. ; bit 0 (0x01): 0 Cylinder 1/4 or 2/3 ?? output compare detection polarity?
  648. ; bit 1 (0x02): 1 Cylinder 1/4 or 2/3 ?? output compare detection polarity?
  649. ; bit 2 (0x04): 0 Cylinder 1/4 or 2/3 ?? output compare detection polarity?
  650. ; bit 3 (0x08): 1 Cylinder 1/4 or 2/3 ?? output compare detection polarity?
  651. ; bit 4 (0x10): 0
  652. ; bit 5 (0x20): 0
  653. ; bit 6 (0x40): 0 1 indicate that the outCompInt3 interrupt is pending/has been activated???
  654. ; bit 7 (0x80): 0
  655. t2_outCmpWr .EQU $001b ;:$001c ; Dual of $0b:$0c, Output compare register, value is compared to t1t2_clk and when a match occurs, injector ports are loaded with the values indicated in t2_csr. seems 2 or 3 successive value can be written (injector activation and deactivation times...)
  656. t2_inCapt .EQU $001d ;:$001e ; Dual of $0d:$0e, Airflow sensor input capture register. Contains the value of t1t2_clk when an airflow sensor pulse edge detected
  657. adc_ctl .EQU $001f ; ADC control; [bit 3 = start bit?, bit 2:0 = channel select ]???
  658. ; bit 0 (0x01): c0 [c2:c1:c0] is the port number to use aas input to the A/D converter
  659. ; bit 1 (0x02): c1
  660. ; bit 2 (0x04): c2
  661. ; bit 3 (0x08): Start bit, set to 1 to start A/D conversion
  662. ; bit 4 (0x10): ?
  663. ; bit 5 (0x20): ?
  664. ; bit 6 (0x40): ?
  665. ; bit 7 (0x80): ?
  666. adc_data .EQU $0020 ; 8 bit A to D converter result data
  667. L0021 .EQU $0021 ; Unused?
  668. L0022 .EQU $0022 ; Unused?
  669. L0023 .EQU $0023 ; Unused?
  670. L0024 .EQU $0024 ; Init to 0?
  671. L0025 .EQU $0025 ; Unused?
  672. rti_ctl .EQU $0026 ; Timer control and status register for real time interrupt? init to $4D = 0100 1101
  673. ; bit 0 (0x01): ?
  674. ; bit 1 (0x02): ?
  675. ; bit 2 (0x04): ?
  676. ; bit 3 (0x08): ?
  677. ; bit 4 (0x10): ?
  678. ; bit 5 (0x20): ?
  679. ; bit 6 (0x40): Set to 1 to enable rti interrupts?
  680. ; bit 7 (0x80): ?
  681. rti_freq .EQU $0027 ; Real time interrupt frequency setting: Freq = 125000/(256-x) where x is the content of rti_freq
  682. L0028 .EQU $0028 ; Unused?
  683. t3_clock1 .EQU $0029 ;:$002a ; Readable counter. Frequency seems to be 250KHz (2MHz/8).
  684. t3_outCmpWr .EQU $002b ;:$002c ; Writable output compare register for counters at $0029:$002A and $002D:$002E
  685. ; Seems to be double buffered...
  686. t3_clock2 .EQU $002d ;:$002e ; Dual of $0029. I think it always has the same value as t3_clock1 but ipon a cas interrupt, the code decides between t3_clock1 and t3_clock2???
  687. port6 .EQU $002f ; Port 6 (all output, no data direction register?)
  688. ; bit 0 (0x01): out - Write 1 to reset instant knock count???
  689. ; bit 1 (0x02): out - ??? Set to 0 when rpm>4688rpm, set to 1 when rpm<4600, could be some kind of ECU board filter setting for the knock sensor???
  690. ; bit 2 (0x04): out - Boost gauge output
  691. ; bit 3 (0x08): out - Check engine (CE) light
  692. ; bit 4 (0x10): out - Reset to 0 to activate purge solenoid?
  693. ; bit 5 (0x20): out - Toggled at F924 if main loop frequency >20Hz, could be tied to ECU reset in case of trouble (COP clock)
  694. ; bit 6 (0x40): out - Not used?
  695. ; bit 7 (0x80): out - Not used?
  696.  
  697. ;------------------------------
  698. ; Block of 16 probably unused
  699. ; microcontroller registers???
  700. ;------------------------------
  701. L0030 .EQU $0030 ; Unused
  702. L0031 .EQU $0031 ; Unused
  703. L0032 .EQU $0032 ; Unused
  704. L0033 .EQU $0033 ; Unused
  705. L0034 .EQU $0034 ; Unused
  706. L0035 .EQU $0035 ; Unused
  707. L0036 .EQU $0036 ; Unused
  708. L0037 .EQU $0037 ; Unused
  709. L0038 .EQU $0038 ; Unused
  710. L0039 .EQU $0039 ; Unused
  711. L003a .EQU $003a ; Unused
  712. L003b .EQU $003b ; Unused
  713. L003c .EQU $003c ; Unused
  714. L003d .EQU $003d ; Unused
  715. L003e .EQU $003e ; Unused
  716. L003f .EQU $003f ; Unused
  717.  
  718.  
  719.  
  720. ;***************************************************************
  721. ;*
  722. ;*
  723. ;* Block of RAM used to preserve settings when the ECU is off
  724. ;* (This block is not cleared to 0 when the ECU is powered-on)
  725. ;*
  726. ;*
  727. ;***************************************************************
  728. ftrim_low .EQU $0040 ; Fuel trim low (.78x)%
  729. ftrim_mid .EQU $0041 ; Fuel trim mid (.78x)%
  730. ftrim_hi .EQU $0042 ; Fuel trim high (.78x)%
  731. ftrimCntr .EQU $0043 ; Fuel trim counter. This counter is increased/decreased by 5 (+/-5 at 40Hz) whenever a fuel trim is below/above o2Fbk threshold. The fuel trim is increased/decreased by 1 whenever this counter rools over, giving an effective update rate of 40Hz/(256/5)=0.78125Hz for the fuel trims update
  732. isc0 .EQU $0044 ;:$0045 ; iscm (isc0 or isc1) are 16 bit long term correction factors/feedback for the isc step adjustment. It is centered at $8000 (100%, no correction). Init to $8c00, A value higher than $8000 indicate that we need to increase the isc step since the current rpm is lower than the desired one
  733. ; The isc step used is increased/decreased by iscm/256 - $80. iscm is updated from the short term iscYn variable.The isc step used is increased/decreased by iscm/256 - $80
  734. ; isc0 is the long term learning variable when A/C is off, 16 bits, see iscPointers function
  735. isc1 .EQU $0046 ;:$0047 ; isc1 is the long term learning variable when A/C is on, 16 bits, see iscPointers function
  736. iscStepCom .EQU $0048 ; isc step complement, shlould be equal to ~iscStepCurr & $7f. Not sure of its utility???
  737. iscStepCurr .EQU $0049 ; Current isc step (x) range of 0 to 120 (or 13x???)
  738. iscPatrnIdx .EQU $004a ; Current ISC pattern index, two lower bits are used as index into t_iscPattern to update port5.6.7 in order to move the ISC spindle...
  739. iscFlags0 .EQU $004b ; Flag register for ISC updating
  740. ; bit 0 (0x01): Set to 1 once the isc calibration is started. This means we initialized iscStepCurr to 135 and set the iscStepTarg to 0. The spindle will therefore be moved to the minimum position irrespective of the starting position, which will allow us to know its real position... Reset to 0 once calibration is finished and ISC is back to iscStepCurr=6
  741. ; bit 1 (0x02): Set to 1 once the isc calibration is finished. i.e. once iscStepCurr reached 0. See bit 0. Reset to 0 once calibration is finished and ISC is back to iscStepCurr=6
  742. ; bit 2 (0x04): Set when basic idle speed adjustment mode is active
  743. ; bit 3 (0x08): Set to 1 when a fixed isc step is used because the engine is running but we are not receiving airflow sensor interrupts.
  744. ; bit 4 (0x10): Set to 1 when a fixed isc step is used because the ECU is about to loose power
  745. ; bit 5 (0x20): Set to 1 when ISC min calibration need to be performed, i.e. move the spindle 135 steps toward 0, that ensures the spindle is positionned at the minimum position, wherever we started from... Reset to 0 once calibration is finished and ISC is back to iscStepCurr=6
  746. ; bit 6 (0x40): Set to 1 when the ISC max calibration has been performed, see bit 7
  747. ; bit 7 (0x80): Set to 1 when ISC max calibration need to be performed. Max calibration is achieved by setting iscStepTarg to 135, wait for iscStepCurr to reach 135 (higher than max usable valu of 120) and then set iscStepCurr to 120 since this is the max usable value
  748. stFaultHi .EQU $004c ; Stored faults, High byte. Notice we say its high byte because it is the ECU convention to store high byte before low byte and it is also used that way in the code
  749. stFaultLo .EQU $004d ; Stored faults, Low byte.
  750. faultHi .EQU $004e ; Faults, high byte. Notice we say its high byte because it is the ECU convention to store high byte before low byte and it is also used that way in the code
  751. faultLo .EQU $004f ; Faults, low byte
  752. o2BadCnt .EQU $0050 ; Used to test the o2 sensor, 0 when 02 sensor not in fault or not tested, 1 or greater when o2 sensor is bad. Can only increase by 1 each time the ECU is turned on and sensor is tested
  753. egrtBadCnt .EQU $0051 ; Used to test the egrt sensor, 0 when egrt sensor not in fault or not tested, 1 or greater when egrt sensor is bad. Can only increase by 1 each time the ECU is turned on and sensor is tested
  754. octane .EQU $0052 ; Octane value used in timing advance calculation with min 0(bad fuel...), max 255 (no knock). Updated at 2.5Hz from knockSum under specific circumstances (decremented by 1 if knocksum>5, incremented by 1 if knocksum<3)
  755. knockFlags .EQU $0053 ; Flags related to knock sensor
  756. ; bit 0 (0x01):
  757. ; bit 1 (0x02):
  758. ; bit 2 (0x04):
  759. ; bit 3 (0x08):
  760. ; bit 4 (0x10):
  761. ; bit 5 (0x20):
  762. ; bit 6 (0x40): Set to 1 when engine has been runnning for more than 1 sec
  763. ; bit 7 (0x80): Set to 1 when airVol>$49, used to know whether engine is under high or loaw load for knockSum and knockSum decay calculations
  764. L0054 .EQU $0054 ; UNUSED?
  765. config1 .EQU $0055 ; Configuration flags depending on config resistors, Loaded with t_strap1[port4& (#$03 << 1)]
  766. ; bit 0 (0x01):
  767. ; bit 1 (0x02):
  768. ; bit 2 (0x04):
  769. ; bit 3 (0x08):
  770. ; bit 4 (0x10):
  771. ; bit 5 (0x20):
  772. ; bit 6 (0x40):
  773. ; bit 7 (0x80):
  774. config2 .EQU $0056 ; Configuration flags depending on config resistors, Loaded with t_strap1[port4& (#$03 << 1)+1]
  775. ; bit 0 (0x01):
  776. ; bit 1 (0x02):
  777. ; bit 2 (0x04):
  778. ; bit 3 (0x08):
  779. ; bit 4 (0x10):
  780. ; bit 5 (0x20):
  781. ; bit 6 (0x40):
  782. ; bit 7 (0x80):
  783.  
  784.  
  785.  
  786. ;***************************************************************
  787. ;*
  788. ;*
  789. ;* RAM, cleared to 0 when the ECU is powered-on
  790. ;*
  791. ;*
  792. ;***************************************************************
  793. ramClearStart .EQU $0057
  794. temp1 .EQU $0057 ;
  795. temp2 .EQU $0058 ;
  796. temp3 .EQU $0059 ;
  797. temp4 .EQU $005a ;
  798. temp5 .EQU $005b ;
  799. temp6 .EQU $005c ;
  800. temp7 .EQU $005d ;
  801. temp8 .EQU $005e ;
  802. temp9 .EQU $005f ;
  803. L0060 .EQU $0060 ; Unused
  804. casFlags0 .EQU $0061 ; Flag register
  805. ; bit 0 (0x01): Bit is set to 1 when rpm(Tcas) >= 505, reset when rpm(Tcas) < 401 (hysteresis)
  806. ; bit 1 (0x02): Old value of bit 0
  807. ; bit 2 (0x04): 1 if rpm(Tcas) > 1775rpm
  808. ; bit 3 (0x08): Old value of bit 2
  809. ; bit 4 (0x10): 1 if rpm(Tcas) > 1540rpm
  810. ; bit 5 (0x20): 1 if rpm(Tcas) > 4801rpm
  811. ; bit 6 (0x40): Set to 1 if timing adjustment mode is active
  812. ; bit 7 (0x80): Unused?
  813. ignFallFlags .EQU $0062 ; Coil ignition scheduling on the cas falling edge
  814. ; bit 0 (0x01): Set to 1 when coil ignition was not scheduled on the CAS
  815. ; rising edge and therefore need to be scheduled on the CAS falling edge?
  816. ; bit 1 (0x02): not used
  817. ; bit 2 (0x04): not used
  818. ; bit 3 (0x08): not used
  819. ; bit 4 (0x10): not used
  820. ; bit 5 (0x20): not used
  821. ; bit 6 (0x40): not used
  822. ; bit 7 (0x80): not used
  823. enerFlags .EQU $0063 ; Coil energization state, bit 0 and 1 are mutually exclusive, they are never set at the same time...
  824. ; Note that when rpm is low, these flags might not be set as indicated (during cranking?)
  825. ; bit 0 (0x01): Set to 1 when coil is currently energized?
  826. ; bit 1 (0x02): Set to 1 when coil energization has been scheduled?
  827. ; bit 2 (0x04): not used
  828. ; bit 3 (0x08): not used
  829. ; bit 4 (0x10): not used
  830. ; bit 5 (0x20): not used
  831. ; bit 6 (0x40): not used
  832. ; bit 7 (0x80): not used
  833. TcasLast0 .EQU $0064 ; TcasLast0:TcasLast1 (250KHz clock) is identical to TcasNew0:TcasNew1 but it has been validated for range. Basically it is the last Tcas value that was valid
  834. TcasLast1 .EQU $0065 ; See TcasLast0
  835. TcasNew0 .EQU $0066 ; TcasNew0:TcasNew1 (250KHz clock) is the new value of Tcas calculated during the CAS interrupt
  836. TcasNew1 .EQU $0067 ; See TcasNew0
  837. casRiseTime0 .EQU $0068 ; casRiseTime0:casRiseTime1 (250KHz clock) is the clock value when the last CAS rising edge interrupt occured
  838. casRiseTime1 .EQU $0069 ; See casRiseTime0
  839. casFallTime0 .EQU $006a ; casFallTime0:casFallTime1 (250KHz clock) is the clock value when the last CAS falling edge interrupt occured
  840. casFallTime1 .EQU $006b ; See casFallTime0
  841. timCas0 .EQU $006c ; The current ignition timing (xx/256*90)degrees referenced to the CAS pulse rising edge (75deg BTDC), [timCas0:timCas1] = 256 * (75.77 - degAdv)/90, calculated from tim61 + $002a
  842. timCas1 .EQU $006d ; See timCas0
  843. ignRelTime0 .EQU $006e ; [ignRelTime0:ignRelTime1] is the current ignition time minus 72us measured in 1/250000 sec (timer clock) and referenced to the CAS rising edge (75deg BTDC). Calculated from timCas0: [ignRelTime0:ignRelTime1] = [TcasNew0:TcasNew1]/2 * [timCas0:timCas1]/256 - $0012
  844. ignRelTime1 .EQU $006f ; See ignRelTime0
  845. ignFallRelTime0 .EQU $0070 ; Similar to ignRelTime0 but measured from te cas falling edge, used to schedule ignition when the timing is high (past the cas falling edge...)
  846. ignFallRelTime1 .EQU $0071 ; See ignFallRelTime0
  847. enerLenX0 .EQU $0072 ; One of the coil energization durations, the one used depends on current conditions...
  848. enerLenX1 .EQU $0073 ; See enerLenX0
  849. enerAbsTime0 .EQU $0074 ; Coil energization absolute time (t3_clock1)
  850. enerAbsTime1 .EQU $0075 ; See enerAbsTime0
  851. ignTime0 .EQU $0076 ; Coil ignition absolute time (t3_clock1)
  852. ignTime1 .EQU $0077 ; See ignTime1
  853. enerAbsTimeNext0 .EQU $0078 ; Coil absolute energization time (ignTime1) but only used when energization of the "next cylinder" is scheduled from the preceeding cylinder coil ignition time...
  854. enerAbsTimeNext1 .EQU $0079 ; See enerAbsTimeNext0
  855. TcasLast128 .EQU $007a ; Set to TcasLast0/128
  856. tdcMask0 .EQU $007b ; tdcMask0:tdcMask1 contains $0204 when TDC signal is active (cylinder 1 or 4) on the CAS rising edge, $0402 otherwise. Toggled on every CAS rising edge
  857. tdcMask1 .EQU $007c ; See tdcMask0
  858. tim61 .EQU $007d ; Current timing (xx/256*90)degrees referenced to 61deg BTDC, tim61 = 256 * (61 - degAdv) / 90, where degAdv is the timing advance in degrees. Calculated from tim61Tot0
  859. temp20 .EQU $007e ;
  860. temp21 .EQU $007f ;
  861. temp22 .EQU $0080 ;
  862. temp23 .EQU $0081 ;
  863. temp24 .EQU $0082 ;
  864. tdcCasCount .EQU $0083 ; CAS rising edge counter when key is not in start, incremented on every CAS rising edge up to a maximum value of 6, used in TDC synch. operation
  865. T40s_casInt .EQU $0084 ; Initialized to 1.275sec on every CAS rising edge interrupt and decremented in first subroutine at ~40Hz. Will reach 0 (expire) only when no CAS interrupt was received for over 1.275sec, i.e. engine is really not rotating or something is wrong?
  866. coilChkFlags .EQU $0085 ; Flag register used to validate the ignition signal using the ignition coil sensing circuit
  867. ; bit 0 (0x01): Injector 1, set to 1 to indicate that the injector can be used, 0 indicate injector is disabled because ignition is not happening on the corresponding cylinder
  868. ; bit 1 (0x02): Injector 3, set to 1 to indicate that the injector can be used, 0 indicate injector is disabled because ignition is not happening on the corresponding cylinder
  869. ; bit 2 (0x04): Injector 4, set to 1 to indicate that the injector can be used, 0 indicate injector is disabled because ignition is not happening on the corresponding cylinder
  870. ; bit 3 (0x08): Injector 2, set to 1 to indicate that the injector can be used, 0 indicate injector is disabled because ignition is not happening on the corresponding cylinder
  871. ; bit 4 (0x10):
  872. ; bit 5 (0x20): Set to 1 when engine is running and rpm<5000 and 8V<=battRaw<=18V, meaning we can proceed with checking the ignition
  873. ; bit 6 (0x40):
  874. ; bit 7 (0x80): Set to 1 when we detected that several ignition signals were missing, ignition is not working properly.
  875. p4Latched .EQU $0086 ; Loaded with port4 and checked for bit #$04 in CAS interrupt
  876. timAdjFlags .EQU $0087 ; Timing adjustment mode flags
  877. ; bit 0 (0x01): Set when rpm31>2000rpm, reset when rpm31 goes lower than 1813rpm (hysteresis)
  878. ; bit 1 (0x02):
  879. ; bit 2 (0x04):
  880. ; bit 3 (0x08):
  881. ; bit 4 (0x10):
  882. ; bit 5 (0x20):
  883. ; bit 6 (0x40):
  884. ; bit 7 (0x80): Set to 1 when timing adjustment mode is active (timing terminal is grounded but the ECU test mode terminal is not grounded
  885. tim61Tot0 .EQU $0088 ; New target timing (xx/256*90)degrees referenced to 61deg BTDC. knockSum is added to this value in order to retard timing further and then a maximum rate of change of 22.5deg/iteration is applied. The result becomes the new timing to apply (tim61 and timCas0:timCas1). Calculated from advTotal
  886. enerLen .EQU $0089 ; Coil energization time as loaded from the t_enerLen(battRaw) table. Actual energization time used might be different, longer...
  887. timingAdv .EQU $008a ; Current timing advance, (x-10)degrees, timingAdv = degAdv+10, Calculated from tim61
  888. knockSum .EQU $008b ; Current knock sum value
  889. T200s_knock .EQU $008c ; Knock attenuation timer decremented at 200Hz and looping at 1.67Hz or 100Hz depending on airVol, knockSum is decremented by 1 every time this timer expires
  890. airCnt0 .EQU $008d ; [airCnt0:airCnt1:airCnt2] is the exponentially averaged 24 bit air count (input is 16 bit [airCntNew0:airCntNew1]*256)
  891. airCnt1 .EQU $008e ; See airCnt0
  892. airCnt2 .EQU $008f ; See airCnt0
  893. airCntNew0 .EQU $0090 ; airCntNew0:airCntNew1 is the 16 bits air count used as input to [airCnt0:airCnt1:airCnt2]. It is equal (N+r) * $9c where N is the number of airflow sensor pulse counted by the mas interrupt between each cas interrupt (1 cas interrupt for every cylinder cycle, 4 per every 2 engine rotations) r<=1 is a "remainder" proportional to the time elapsed since the last interrupt...
  894. airCntNew1 .EQU $0091 ; See airCntNew0
  895. oldAirCnt0 .EQU $0092 ;:$0093 ; This is the old value of airCnt0:airCnt1 used to compute some kind of air count derivative
  896. airDiffPos .EQU $0094 ; Contains airCnt0-oldAirCnt0 when the difference is positive, This is kind of the derivative of air count which is positive when air count is increasing (acceleration)
  897. airDiffNeg .EQU $0095 ; Contains abs(airCnt0-oldAirCnt0) when the difference is negative (contains oldAirCnt0-airCnt0...). This is kind of the derivative of air count which is negative when air count is decreasing (decceleration)
  898. t1_lastCas .EQU $0096 ;:$0097 ; Latest value of t1_inCapt when CAS interrupt was called
  899. t2_lastMas .EQU $0098 ;:$0099 ; Latest value of t2_inCapt when MAS interrupt was called
  900. t2_diff8 .EQU $009a ;:$009b ; Time between 2 edges (2 edges per pulse...) of the airflow sensor with timer based rounding (see code), calculated on each mas interrupts from t2_inCapt/8
  901. airQuantum .EQU $009c ; This value ($9c) is the the "amount of air" corresponding to 1 airflow sensor pulse. Using a non unitary value allows the ECU to interpolate the airflow between pulses, i.e. if at the time we calculate airflow we are at 2/3 in between two pulses then we add 2/3 of airQuantum...
  902. ; It is added to [airCntNew0:airCntNew1] on each mas interrupt call (accumulates N times $9C...). A ratio is also applied to this value when it is added to [airCntNew0:airCntNew1] for the last time (partial count in between pulses) before airCnt0 is calculated.
  903. L009d .EQU $009d ; Not used?
  904. masCasFlags .EQU $009e ; Flag register
  905. ; bit 0 (0x01): Bit is set when the CAS rising edge interrupt code is executed to flag the event to the main loop. Flag is read from main loop to update rpmX4Filt and then reset
  906. ; bit 1 (0x02):
  907. ; bit 2 (0x04):
  908. ; bit 3 (0x08):
  909. ; bit 4 (0x10):
  910. ; bit 5 (0x20):
  911. ; bit 6 (0x40):
  912. ; bit 7 (0x80): Scaling for the airflow sensor pulse counting. Set to 0 when we count both the rising and falling edge of the airflow sensor pulse. Set to 0 in case we count only the rising edges (or only the falling ones)
  913. airFiltFact .EQU $009f ; airCnt0 exponential averaging factor with alpha = airFiltFact/256, 0<=alpha<=1, basically used to filter the air count: new airCnt0 = alpha * old airCnt0 + (1-alpha)*newAirCntValue, possible values in the code are $b3(70%), $d1(82%) or $e4(89%)
  914. airCntMax .EQU $00a0 ; Air count based on rpm, ect and iat, 8*airCntMax is used as a maximum on airCnt0 or when engine not rotating/starting to rotate
  915. accEnr .EQU $00a1 ; Acceleration enrichment (100x/255)%. This value is actually updated with min(airCnt0-oldAirCnt0,$48) under acceleration, see code. Max value is $48 from code
  916. state3 .EQU $00a2 ; Flag register
  917. ; bit 0 (0x01): Copied from same bit in state1 (1=startingToCrank)
  918. ; bit 1 (0x02): Copied from same bit in state1 (1=no pulse accumulator interrupts)
  919. ; bit 2 (0x04): Set when RPM exceeds threshold (rev limiter)
  920. ; bit 3 (0x08): Copied from same bit in state1 (1=rotatingStopInj)
  921. ; bit 4 (0x10): Copied from same bit in state1 (1=notRotating)
  922. ; bit 5 (0x20): Set if rotatingStopInj and not runningFast
  923. ; bit 6 (0x40):
  924. ; bit 7 (0x80): Set to injFlags0.7 (1 when startingToCrankColdEngine)
  925. injFactor .EQU $00a3 ;:$00a4 ; Global injector factor used to calculate injPw from [airCnt0:airCnt1],
  926. ; injFactor = 16*totMasComp * injComp/128 * [workFtrim + o2FuelAdj + 2*$80]/512 * iatCompFact/128 * baroFact/128 * openLoopEnr/128 * coldTempEnr/128 * (2*enrWarmup + $80)/128
  927. oldReedVal .EQU $00a5 ; Old value of the reed switch sensor
  928. deadTime .EQU $00a6 ; Injector deadtime in increment of 24uS (depends on current batteryVoltage)
  929. injPw .EQU $00a7 ;:$00a8 ; 16 bit injector pulse width in microseconds. Logger reports high and low bytes: (.256 highByte)mS
  930. inj1_offT .EQU $00a9 ;:$00aa ; Injector #1? deactivation time (relative to timer t1t2_clk)
  931. inj3_offT .EQU $00ab ;:$00ac ; Injector #3? deactivation time (relative to timer t1t2_clk)
  932. inj4_offT .EQU $00ad ;:$00ae ; Injector #4? deactivation time (relative to timer t1t2_clk)
  933. inj2_offT .EQU $00af ;:$00b0 ; Injector #2? deactivation time (relative to timer t1t2_clk)
  934. last_t1t2_clk .EQU $00b1 ; Initialized to t1t2_clk/256 on CAS falling edge, every one of them???
  935. injToAct .EQU $00b2 ; Indicate which injectors are currently active or should be activated, Set to 1 for an active injector
  936. ; bit 0 (0x01): Inj 1?
  937. ; bit 1 (0x02): Inj 3?
  938. ; bit 2 (0x04): Inj 4?
  939. ; bit 3 (0x08): Inj 2?
  940. ; bit 4 (0x10):
  941. ; bit 5 (0x20):
  942. ; bit 6 (0x40):
  943. ; bit 7 (0x80):
  944. tdcCasFlags .EQU $00b3 ; Init to 5
  945. ; bit 0 (0x01): c0, c2:c1:c0 used as a down counter (on every CAS pulse falling edge) initialized with 5. Reset to 0 when the CAS pulse falling edge correpond to the cylinder #1 TDC pulse (see bit 7)
  946. ; bit 1 (0x02): c1
  947. ; bit 2 (0x04): c2
  948. ; bit 3 (0x08): Set to the last value of TDC bit on port3. 2
  949. ; bit 4 (0x10):
  950. ; bit 5 (0x20):
  951. ; bit 6 (0x40):
  952. ; bit 7 (0x80): Set to 1 when cylinder #1 TDC is detected on the CAS falling edge. Set to 1 when we detect that TDC bit on port3.2 has changed from 1 to 0 (falling edge) from one CAS falling edge to the other. That basically indicate cylinder #1 TDC
  953. casCylIndex .EQU $00b4 ; Cas current cylinder index (0,1,2,3 -> cyl #2,#1,#3,#4). Counter looping from 0 to 3 and increased on every CAS falling edge. re-init to 0 when TDC of cylinder #1 is detected (tdcCasFlags.7 set).
  954. ; bit 0 (0x01):
  955. ; bit 1 (0x02):
  956. ; bit 2 (0x04):
  957. ; bit 3 (0x08):
  958. ; bit 4 (0x10):
  959. ; bit 5 (0x20):
  960. ; bit 6 (0x40):
  961. ; bit 7 (0x80):
  962. newInjToAct .EQU $00b5 ; Indicate which injector should be activated (also, bit 7 is set when doing simultaneous injection). Mostly updated on the CAS falling edge
  963. ; bit 0 (0x01): Inj 1?
  964. ; bit 1 (0x02): Inj 3?
  965. ; bit 2 (0x04): Inj 4?
  966. ; bit 3 (0x08): Inj 2?
  967. ; bit 4 (0x10): Inj 5/6?
  968. ; bit 5 (0x20): Inj 5/6?
  969. ; bit 6 (0x40):
  970. ; bit 7 (0x80): Set to 1 when when we should be doing simultaneous injection on all 4 cylinders, 0 indicate sequential injection
  971. tdcCheck .EQU $00b6 ; Init to 8 on the cas falling edge of the cylinder #1 TDC, decremented by 1 on every cas falling edge. Used to check that TDC sensor is working correctly, it should never reach 0...
  972. oldInjToAct .EQU $00b7 ; Old value of injToAct (before it was updated)
  973. injToTest .EQU $00b8 ; The current injector to test for proper operation (set to 1 to test), 1 bit per injector. Testing proceed from bit 0 to bit 3. We stay on the same injector if it is found to be bad, see around L1884
  974. ; bit 0 (0x01): Inj 1?
  975. ; bit 1 (0x02): Inj 3?
  976. ; bit 2 (0x04): Inj 4?
  977. ; bit 3 (0x08): Inj 2?
  978. ; bit 4 (0x10):
  979. ; bit 5 (0x20):
  980. ; bit 6 (0x40):
  981. ; bit 7 (0x80):
  982. injBad .EQU $00b9 ; Injector testing flags
  983. ; bit 0 (0x01): Set to 1 when one of the injector is not working correctly based on injector feedback bit, see injToTest
  984. ; bit 1 (0x02): Not used
  985. ; bit 2 (0x04): Not used
  986. ; bit 3 (0x08): Not used
  987. ; bit 4 (0x10): Not used
  988. ; bit 5 (0x20): Not used
  989. ; bit 6 (0x40): Not used
  990. ; bit 7 (0x80): Not used
  991. obdInjCmd .EQU $00ba ; processing of OBD code bit 0 to 5 correspond to injectors being turned on/off
  992. ; bit 0 (0x01): Inj. 1, Set to 0 if injector is currently turned off by obd command, 1 in normal operation
  993. ; bit 1 (0x02): Inj. 3, See bit 0
  994. ; bit 2 (0x04): Inj. 4, See bit 0
  995. ; bit 3 (0x08): Inj. 2, See bit 0
  996. ; bit 4 (0x10): Inj 5/6 See bit 0
  997. ; bit 5 (0x20): Inj 5/6 See bit 0
  998. ; bit 6 (0x40): Not used
  999. ; bit 7 (0x80): Not used
  1000. rtiCnt .EQU $00bb ; free counter increased on every real time interrupt (~800Hz), used to execute some functions only 1 out of N times (check count value)
  1001. rtiCnt48 .EQU $00bc ; counter increased on every real time interrupt, maximum value is 2F (47), period 48.
  1002. rtiReedFlags .EQU $00bd ; Flag register
  1003. ; bit 0 (0x01): Bit is set in real time int. at 40Hz when T200_40Hz reach 0 (T200_40Hz loop from 5 to 0 at 200Hz)
  1004. ; bit 1 (0x02): not used?
  1005. ; bit 2 (0x04): not used?
  1006. ; bit 3 (0x08): not used?
  1007. ; bit 4 (0x10): not used?
  1008. ; bit 5 (0x20): not used?
  1009. ; bit 6 (0x40): not used?
  1010. ; bit 7 (0x80): Latest reed switch value
  1011.  
  1012. ;-------------------------------------------------
  1013. ; $be-$c2, series of timers (counting down to 0)
  1014. ; (see L1929) decremented in real time interrupt
  1015. ; at ~200Hz
  1016. ;-------------------------------------------------
  1017. T200_40Hz .EQU $00be ; Loops from from 5 to 0 producing 40Hz for main loop (set rtiReedFlags.0)
  1018. T200_casRise .EQU $00bf ; Used by CAS interrupt as a timeout to validate the time in between CAS interrupts when RPM is very low and 16 bit timers roll over. Timer initialized on every cas rising edge
  1019. T200_casFall .EQU $00c0 ; Used by CAS interrupt as a timeout to validate the time in between CAS interrupts when RPM is very low and 16 bit timers roll over. Timer initialized on every cas falling edge
  1020. T200_mas .EQU $00c1 ; Used by the mas subroutine to know when the pulses are getting too close together (and we need to apply scaling). Re-initialized to 130ms on every airflow sensor interrupt (every pulse received)
  1021. T200_cop .EQU $00c2 ; Used to toggle port6.5 if main loop executes at more than 20Hz, could be some sort of COP to reset ECU in case main loop goes slower than 20Hz.
  1022.  
  1023. oldTps1 .EQU $00c3 ; Set to "old" TPS value for comparison with new during cranking?
  1024. vssCnt1 .EQU $00c4 ; Counter Initialized to $c8 every time the reed switch change value, decreased at real time int. frequency down to 0; Will be 0 only when when speed is lower than X (very slow speed?)
  1025. vssCnt2 .EQU $00c5 ; Counter used for speed sensor calculation, initialized to $E2, decreased on every call, speed = $E2-current value of this register
  1026. vss .EQU $00c6 ; "Vehicle speed sensor", actually computed from the reed switch transitions. Value is the period in 1/400sec of one complete reed switch square wave, approx 40cm. Speed in km/h is approximately given by 3.6*400*0.4/xx
  1027. oldTps2 .EQU $00c7 ;
  1028. tpsDiffMax1 .EQU $00c8 ; Maximum positive rate of change of tpsRaw seen during 1 main loop execution. (high value means driver is stepping on the gas, value of 0 means throttle is at constant position or decreasing). Set to the maximum value of tpsRaw-oldTps2 (updated at 100Hz)
  1029. tempFlagTps .EQU $00c9 ; Used as a temp flag during port2.2 activation/deactivation (aiflow sensor active filter reset???). Set to $ff when tpsRaw has increased by more than 1.5% and is between 26%-50%, $00 otherwise
  1030. L00ca .EQU $00ca ; Init to 6 but never used. Notice it is located in front of the 8 memories used to store ADC values? It also correspond to obd code $ca which erases all fault codes...
  1031. ectRaw .EQU $00cb ; Engine coolant temperature, see curve at beginning of file
  1032. iatRaw .EQU $00cc ; Raw intake air temperature, see curve at beginning of file
  1033. baroRaw .EQU $00cd ; Atmostpheric pressure: (.00486x)bar
  1034. o2Raw .EQU $00ce ; Oxygen sensor (.0195x)v.
  1035. egrtRaw .EQU $00cf ; Exhaust gas recirculation temperature, unknown temperature curve, used this formula for now (-2.7x + 597.7)deg F
  1036. battRaw .EQU $00d0 ; Battery voltage (.0733x)v.
  1037. knockSensor .EQU $00d1 ; Knock sensor
  1038. tpsRaw .EQU $00d2 ; Throttle position sensor (100x/255)%
  1039. ectFiltered .EQU $00d3 ; This is the engine coolant tempterature that has been validated and then filtered to limit its rate of change to a few degrees per sec...
  1040. iatChecked .EQU $00d4 ; Validated intake air temperature
  1041. baroChecked .EQU $00d5 ; Verified barometer voltage, $cd=1bar
  1042. state2 .EQU $00d6 ; bit 0 (0x01): Set if ectRaw >236 OR <5, FAULT VALUE = 30
  1043. ; bit 1 (0x02): Set if iatRaw >234 OR <14, FAULT VALUE = 123
  1044. ; bit 2 (0x04): Set if baroRaw >228 OR <100, FAULT VALUE = 205
  1045. ; bit 3 (0x08): Set when timer T40_mas expires, which means no pulse accumulator interrupt was received for over 0.3s-> no air is getting in or something wrong...
  1046. ; bit 4 (0x10): Engine collant temp related?
  1047. ; bit 5 (0x20): Set to 1 if knock senor is not working???. Set to ^(port4Snap.5)
  1048. ; bit 6 (0x40):
  1049. ; bit 7 (0x80):
  1050. port3Snap0 .EQU $00d7 ; Loaded with port3 with some values reset by code depending on ???,
  1051. ; used in idle speed calc? -> load that need to be considered in calc of idle speed
  1052. ; bit 0 (0x01): IG2 related, 0 when IG2 at +12V, ABS unit? only used used for fuel trim on E931 only, see around Md4d4?
  1053. ; bit 1 (0x02): IG1 related? 0 when IG1 at +12V.
  1054. ; bit 2 (0x04): always reset to 0????
  1055. ; bit 3 (0x08): Set to 1 if power steering pump is on
  1056. ; bit 4 (0x10): AC switch (1=off) -> always set if ??? (config resistor???)
  1057. ; bit 5 (0x20): Park/neutral -> always set by code???
  1058. ; bit 6 (0x40): 0 if key in start?
  1059. ; bit 7 (0x80): idle position switch (1=on)?
  1060. port4Snap .EQU $00d8 ; Snapshot of port4 & 01111000
  1061. ; bit 0 (0x01):
  1062. ; bit 1 (0x02):
  1063. ; bit 2 (0x04):
  1064. ; bit 3 (0x08): 1 when ECU test mode terminal grounded
  1065. ; bit 4 (0x10): 1 when timing terminal grounded
  1066. ; bit 5 (0x20): knock sensor related (set indicates it works...)
  1067. ; bit 6 (0x40): Fuel pump driven feedback?
  1068. ; bit 7 (0x80):
  1069. Tclocks .EQU $00d9 ; State Flags for software counters, Updated from scratch (zero) on every main loop execution
  1070. ; bit 0 (0x01): Set at ~40Hz, set when 40Hz flag from real time interrupt was processed during loop (40Hz counters where decremented if required)
  1071. ; bit 1 (0x02): Set at ~10Hz
  1072. ; bit 2 (0x04): Set at ~2Hz, Used by heart beat mode
  1073. ; bit 3 (0x08): Set at ~0.5Hz
  1074. ; bit 4 (0x10): Not used?
  1075. ; bit 5 (0x20): Not used?
  1076. ; bit 6 (0x40): Not used?
  1077. ; bit 7 (0x80): Not used?
  1078. rpm4 .EQU $00da ;:$00db ; RPM/3.90625
  1079. rpm8 .EQU $00dc ; RPM/7.8125
  1080. rpm31 .EQU $00dd ; RPM/31.25 (engine rpm = RPM31p25 * 31.25)
  1081. airVol16 .EQU $00de ;:$00df ; Air volume, 16 bit, airVol16 = [airCnt0:airCnt1] * masScalar/65536
  1082. airVol .EQU $00e0 ; Air volume, 8 bit, airVol = airVol16/2
  1083. airVolT .EQU $00e1 ; Air volume, 8 bit, airVolT = airVol16/2 * iatCompFact/128
  1084. airVolTB .EQU $00e2 ; Air volume, 8 bit, airVolTB = airVol16/2 * iatCompFact/128 * baroFact/128
  1085. airVolB .EQU $00e3 ; Air volume, 8 bit, airVolB = airVol16/2 * baroFact/128
  1086. mafRaw .EQU $00e4 ; 8 bit airflow sensor pulse frequency (6.25x)Hz, calculated from mafRaw16 (mafRaw = mafRaw16/64)
  1087. ftrimFlags .EQU $00e5 ; Flag register for fuel trim???
  1088. ; bit 0 (0x01): c0: c1c0 form the current trim range (00=low, 01=mid, 10=high) updated according to mafRaw16
  1089. ; bit 1 (0x02): c1
  1090. ; bit 2 (0x04): Set (E931 only) when speed exceed threshold (24km/h) with hysteresis,
  1091. ; bit 3 (0x08): Set (E931 only) if port3Snap0.0 & port3.0 are both set on E931 when speed exceed 24km/h?
  1092. ; bit 4 (0x10): Set when rpm > L1983(xx) ~1000rpm with hysteresis
  1093. ; bit 5 (0x20):
  1094. ; bit 6 (0x40):
  1095. ; bit 7 (0x80): Set when airVolT >24, reset when airVolT<=15. Theshold is 19.5 with +/-4.5 hysteresis
  1096. state1 .EQU $00e6 ; State flags mainly used to track engine start-up stages and running condition (not rotating, startingToCrank, etc.). Bits 0 to 4 will be clear when engine is running normally
  1097. ; bit 0 (0x01): stage1 (startingToCrank?): 1 indicate engine is just rotating but no TDC signal seen yet????, reset to 0 once CAS/TDC??? or engine rpm>~400rpm
  1098. ; bit 1 (0x02): no pulse accumulator interrupts ?: 1 indicate we did not receive a valid pulse accumulator interrupts for a long time, see state2
  1099. ; bit 2 (0x04): stage3 (runningFast): 1 indicate the ECU has detected that the engine rpm was too high for current conditions?
  1100. ; bit 3 (0x08): stage2 (rotatingStopInj): 1 indicate engine should be running or be started but something is preventing us from doing fuel injection (fuel cut, CAS not working, etc.). Injection could still proceed if runningFast is set
  1101. ; bit 4 (0x10): stage0 (notRotating): 1 indicate state1 was calculated but nothing to report, set to 1 on init subr. reset to 0 when engine is rotating
  1102. ; bit 5 (0x20): state1Calculated: 1 indicate state 1 was calculated, never reset?
  1103. ; bit 6 (0x40): Not used?
  1104. ; bit 7 (0x80): closedLoop: 1 indicate closed loop mode, 0 indicate open loop
  1105. injFlags0 .EQU $00e7 ; Flags related to injectors
  1106. ; bit 0 (0x01): Flag is 0 on reset (meaning injectors interrupts not yet initiated?) and set to 1 once sInjPw is initialized. If required, first interrupt for injectors is also scheduled when initializing this flag to 1
  1107. ; bit 1 (0x02): ???
  1108. ; bit 2 (0x04): Set when rpm>=437.5
  1109. ; bit 3 (0x08): ???
  1110. ; bit 4 (0x10):
  1111. ; bit 5 (0x20): Set when temperature(ectFiltered) < -8degC, updated only when engine is notRotating
  1112. ; bit 6 (0x40):
  1113. ; bit 7 (0x80): Set to 1 if startingToCrankColdEngine. Fuel should be injected simultaneously in all cylinders twice per rotation (every cas)
  1114. closedLpFlags .EQU $00e8 ; Flags relaed to closed loop mode, 02 sensor, fuel trims
  1115. ; bit 0 (0x01): Set to 1 when the air volume (airVolTB) is too high to use closed loop mode (first threshold)
  1116. ; bit 1 (0x02): Set to 1 when we should be using closed loop mode??? (might not use it anyway...)
  1117. ; bit 2 (0x04):
  1118. ; bit 3 (0x08):
  1119. ; bit 4 (0x10):
  1120. ; bit 5 (0x20):
  1121. ; bit 6 (0x40): o2 sensor bad flag. Set to 1 when the o2 sensor voltage did not switch from lean to rich or rich to lean for a certain amount of time in closed loop. Also set to 1 if notRotating
  1122. ; bit 7 (0x80): rich/lean flag, set to 1 o2Raw >= 0.6v (rich), Set to 0 if o2Raw < 0.6v (lean), updated once o2 sensor has warmed-up
  1123. o2Fbk .EQU $00e9 ;:$00ea ; Oxygen feedback trim (16 bits actually used, most of the time only highest byte is used...) (.78x)% -> 100% = $80
  1124. o2Fbk_dec .EQU $00eb ; o2Fbk is decreased using this value when in closed loop and running rich
  1125. o2Fbk_inc .EQU $00ec ; o2Fbk is increased using this value when in closed loop and running lean
  1126. iscY0 .EQU $00ed ; iscYn variables are short term correction factors/feedback for the isc step adjustment. It is centered at $80 (100%, no correction). A value higher than $80 indicate that we need to increase the isc step since the current rpm is lower than the desired one
  1127. ; The isc step used is increased/decreased by iscYn-$80
  1128. ; iscY0 is the ISC learning variable when A/C is off and PS is off, see iscPointers function. Value of $80=100%
  1129. iscY1 .EQU $00ee ; iscY1 is the ISC learning variable when A/C is on and PS is off, see iscPointers function. Value of $80=100%
  1130. iscY2 .EQU $00ef ; iscY2 is the ISC learning variable when PS is on, see iscPointers function. Value of $80=100%
  1131. iscStepMax .EQU $00f0 ; Maximum value applied to iscStepCurr in code
  1132. port3Snap1 .EQU $00f1 ; Loaded with port3Snap0 with some values set by code depending on ???,
  1133. ; bit 0 (0x01): IG2 related, 0 when IG2 at +12V, ABS unit? only used used for fuel trim on E931 only, see around Md4d4?
  1134. ; bit 1 (0x02): IG1 related? 0 when IG1 at +12V.
  1135. ; bit 2 (0x04): Set to 1 if vssCnt1 != 0 (car speed > 2.9km/h???)
  1136. ; bit 3 (0x08): Set to 1 if power steering pump is on
  1137. ; bit 4 (0x10): AC switch (1=off)
  1138. ; bit 5 (0x20): Park/neutral -> always set by code
  1139. ; bit 6 (0x40): 0 if key in start?
  1140. ; bit 7 (0x80): idle position switch (1=on)?
  1141. oldP3Snap1 .EQU $00f2 ; Old value of port3Snap1
  1142. iscLrnFlags .EQU $00f3 ; Isc leanrning flags, all flags are reset to 0 in basic idle speed adjustment mode. All flags except bit 0 are reset to 0 when notRotating or startingToCrank
  1143. ; bit 0 (0x01): Set to 1 when engine is notRotating or startingToCrank, reset to 0 when engine is running
  1144. ; bit 1 (0x02):
  1145. ; bit 2 (0x04): Set to 1 when the engine is running too slow? i.e. temperature(ectFiltered) > 55degC, rpm8 < 500rpm, engine is running, T40_acOnTrans is expired
  1146. ; bit 3 (0x08):
  1147. ; bit 4 (0x10): Set to 1 when conditions are good to update the isc leanrning variables.
  1148. ; bit 5 (0x20): Set to 1 when iscStStall has been updated, i.e. when idle switch is off and iscFlags1.7 = 0 and rpm8>=500. Reset to 0 when iscStStall is reset to 0???
  1149. ; bit 6 (0x40):
  1150. ; bit 7 (0x80):
  1151. iscFlags1 .EQU $00f4 ; Flag register
  1152. ; bit 0 (0x01): Set to 1 when engine not rotating or is running (basic idle speed adjustment mode is not active). Reset to 0 when key in start and iscStTargSpec = iscStepCurr
  1153. ; bit 1 (0x02):
  1154. ; bit 2 (0x04):
  1155. ; bit 3 (0x08):
  1156. ; bit 4 (0x10):
  1157. ; bit 5 (0x20): Set to 1 when engine StartingToCrank and temperature(iat) < 75degC. Only changed during startingToCrank. Used is setting isc step during cold engine startup
  1158. ; bit 6 (0x40):
  1159. ; bit 7 (0x80): Set to 1 when tps has been high and airVol low for more than 0.5sec (tpsRaw >= 86% and airVol < $3a)
  1160. T_maxAdv .EQU $00f5 ; E931 only, used to ramp down the effect of maxAdv
  1161. maxAdv .EQU $00f6 ; E931 only, maximum value of timing advance timingOct for E931 when engine is runningFast (timingOct is clipped to that value), T_maxAdv is used to ramp-down its effect with time
  1162. L00f7 .EQU $00f7 ; Unused?
  1163. varFlags0 .EQU $00f8 ; Various flags...
  1164. ; bit 0 (0x01): Used in A/C cutoff for AT, 1 indicates TPS exceeded 82% the last time we checked it...
  1165. ; bit 1 (0x02): Hot start flag, set to 1 when startingToCrank and open loop and temperature(iatChecked) >= 60degC and temperature(ectFiltered) >= 93degC (hot start), used to increase fuel enrichement (reduce vapor lock maybe???)
  1166. ; bit 2 (0x04):
  1167. ; bit 3 (0x08):
  1168. ; bit 4 (0x10):
  1169. ; bit 5 (0x20): Second priority, Set to 1 when purge solenoid is to be deactivated since min conditions for normal purge activation are not met
  1170. ; bit 6 (0x40): First priority, Set to 1 when purge solenoid is to be activated by OBD command or normal activation criteria
  1171. ; bit 7 (0x80): Third priority, Set to 1 when purge solenoid should be deactivated. This flag is toggled between 0 and 1 to implement pulsewidth modulation (very long period) of purge solenoid when the other two higher priority flags are not set
  1172. fpsBcsFlags .EQU $00f9 ; Flags related to fuel pressure solenoid and boost control solenoid
  1173. ; bit 0 (0x01):
  1174. ; bit 1 (0x02):
  1175. ; bit 2 (0x04): Set to 1 when the fuel pressure solenoid was just deactivated (set to 1 only when bit 7 goes from 1 to 0). Reset to 0 at any other time
  1176. ; bit 3 (0x08): Set to 1 when ECU decides that fuel pressure solenoid should be activated to reduce vapor lock
  1177. ; bit 4 (0x10): bcs, Set to 1 when mafRaw16 is above $4e ($4a for E932) with hysteresis, low threshold is $38
  1178. ; bit 5 (0x20): bcs, Set to 1 when octane is above $c0 with hysteresis, low threshold is $9a
  1179. ; bit 6 (0x40): bcs
  1180. ; bit 7 (0x80):
  1181. obdFlags .EQU $00fa ; Current state of diagnostic port command/query processing
  1182. ; bit 0 (0x01): 1 toggle bit on every second "subroutine 1" loop
  1183. ; bit 1 (0x02): 1 if serial output on port 2 is initialized.
  1184. ; bit 2 (0x04): ?
  1185. ; bit 3 (0x08): ?
  1186. ; bit 4 (0x10): ?
  1187. ; bit 5 (0x20): ?
  1188. ; bit 6 (0x40): Set to 1 when a new OBD code was stored in obdCode? reset when obdCode has been processed.
  1189. ; bit 7 (0x80): Set to 1 to indicate that a response to the query/command is being sent on the diagnostic port (new requests will be ignored)
  1190. obdActCmd .EQU $00fb ; processing of OBD code, contains which actuator is being currently processed. Set to 0 when actuator is off
  1191. ; bit 0 (0x01): Purge solenoid
  1192. ; bit 1 (0x02): Fuel pump
  1193. ; bit 2 (0x04): Fuel pressure solenoid
  1194. ; bit 3 (0x08): Egr solemoid
  1195. ; bit 4 (0x10): Unused
  1196. ; bit 5 (0x20): Boost control solenoid
  1197. ; bit 6 (0x40): Unused
  1198. ; bit 7 (0x80): Unused
  1199. validFlags .EQU $00fc ; Flag related to the validation of sensors...
  1200. ; bit 0 (0x01): Set to 1 when T40_engRot is expired (no CAS interrupt received for a long time)
  1201. ; bit 1 (0x02): Set to 1 when the condition of the o2 sensor was determined (good or not). Only reset when car key is put in off I think
  1202. ; bit 2 (0x04): Set to 1 when the condition of the egrt sensor was determined (good or not). Only reset when car key is put if off I think
  1203. ; bit 3 (0x08):
  1204. ; bit 4 (0x10):
  1205. ; bit 5 (0x20):
  1206. ; bit 6 (0x40):
  1207. ; bit 7 (0x80): Set to 1 if o2Raw > 0.6V (rich), 0 otherwise
  1208. iscStepTarg .EQU $00fd ; Target ISC step, that's the target value for iscStepCurr
  1209. idleSpdTarg .EQU $00fe ; Current target idle speed (xx*7.8125)rpm based on ect, A/C switch, etc.
  1210. airCntDef .EQU $00ff ; airCntDef*8*256 is the default value of [airCnt0:airCnt1:airCnt2] when mas interrupts are not being received, calculated from rpm, tps, ect, tables
  1211. injPwStart .EQU $0100 ;:$0101 ; The value of injPw used when engine is "rotating" (start-up). Calculated from fixed values (no air count)
  1212. oldFtrimFlg .EQU $0102 ; Old value of ftrimFlags
  1213. accEnrDecay .EQU $0103 ; Acceleration enrichment decay factor. accEnr is multiplied by (1-accEnrDecay/256) on each iteration. Initialized from a table as a function of ect.
  1214. accEnrTimer .EQU $0104 ; Timer used to continue applying acceleration enrichement for 4 iterations after airflow is below minimum threshold (accEnrMinAf).
  1215. accEnrMinAf .EQU $0105 ;:$0106 ; Minimum value of airCnt0 above/below which acceleration/deceleration enrichment should be applied (for acceleration, when airflow goes below, we stop applying acc enrichment after 4 iterations, see accEnrTimer. For decelaration, we stop reducing injPw as soon as we are above threshold)
  1216. decEnr .EQU $0107 ; Deceleration enrichment (100x/255)%. This value is actually updated with min(airCnt0-oldAirCnt0,$ff) under deceleration, see code. Max value is $ff from code
  1217. accEnrFact .EQU $0108 ;:$0109 ; Factor used in increasing injPw during acceleration enrichment
  1218. decEnrFact .EQU $010a ;:$010b ; Factor used in decreasing injPw during deceleration enrichment
  1219. accEnrDiffT .EQU $010c ; Minimum value of (airCnt0-oldAirCnt0) required to update decEnr or accEnr
  1220. accEnrTmr2 .EQU $010d ; Timer used to hold accEnr for 4 or 5 iterations when it is getting small, before decreasing it to 0.
  1221. oldTps3 .EQU $010e ; Old value of tpsRaw calculated at 100Hz, used to compute tpsDiff100
  1222. tpsDiff100 .EQU $010f ; Used to interpolate t_sInjEnr. Correspond to max(tpsRaw-oldTps3,0) calculated at 100Hz, used in the calculation of sInjEnr.
  1223. T200s_sInj .EQU $0110 ; sInjEnr is reset to 0 when this timer expires (0.2sec after conditions don't warrant having sInjEnr anymore)
  1224. sInjEnr .EQU $0111 ; Kind of acceleration fuel enrichement when still using simultaneous injection?
  1225. sInjEnrMax .EQU $0112 ; Maximum value applied to sInjEnr
  1226. sInjTpsMax .EQU $0113 ; sInjEnr is only increased if oldTps3 <= sInjTpsMax
  1227. sInjPw .EQU $0114 ;:$0115 ; Injector pulsewidth used when simulataneous injection is used
  1228. sInjEnrInc .EQU $0116 ; sInjEnr is increased by sInjEnrInc/128 * t_sInjEnr(tpsDiff100) = sInjEnrMax/32 * t_sInjEnr(tpsDiff100) at 100Hz under specific scenario
  1229.  
  1230. ;----------------------------------------------------
  1231. ; $117-$132, series of software timers (counting down to 0)
  1232. ; decremented in subroutine 4 at ~40Hz
  1233. ;----------------------------------------------------
  1234. T40_2hz .EQU $0117 ; set to $14 on init and loop at $14 (produces 2Hz)
  1235. T40_0p5hz .EQU $0118 ; set to $50 on init and loop at $50 (produces 0.5Hz)
  1236. T40_start .EQU $0119 ; Set to $ff when key in start, start counting when key no more in start?
  1237. T40_crank .EQU $011a ; Set to $ff when startingToCrank, starts counting when engine is no more startingToCrank (engine running or other state...)
  1238. T40_baro .EQU $011b ; Used to ignore barometric sensor input if battery<8V and 0.35s after battery>8V (baro sensor sensitive to voltage...). Sensor is ignore when timer is not 0
  1239. T40_stInj0 .EQU $011c ; Starts counting from 1 sec when rotatingStopInj flag is activated. Used to activate T40_stInj1
  1240. T40_stInj1 .EQU $011d ; Initialized to 2 sec when T40_stInj0 expires (ongoing rotatingStopInj for more than 1sec) starts counting when rotatingStopInj is deactivated. This timer is therefore non-zero 1 sec after rotatingStopInj starts and 2 sec after is stops
  1241. T40_o2Fbk .EQU $011e ; Timer will only be 0 when the low trim range will have been selected for more than 4 sec. Used to eventually calculate o2Fbk_dec,o2Fbk_inc, how fast o2 feedback is adjusting...
  1242. T40_ftrim2 .EQU $011f ; Used on E931 as an additional condition to update fuel trims
  1243. T40_engRot .EQU $0120 ; Kind of an "engine rotating" flag, This timer is re-initialized to 0.6s or 1.2s on every CAS interrupt, will reach 0 only if no CAS int. is received (engine not rotating or very slowy) for more than that time (rpm<0.83/K???)
  1244. T40_mas .EQU $0121 ; This timer is periodically initialized to 12 (0.3s) and will reach 0 only if no mas interrupt is received for that long (no air is getting in or something is wrong...)
  1245. T40_fuelCut .EQU $0122 ; Fuel cut timer, fuel cut is applied only when this timer reach 0: After air flow threshold is exceeded for more than 1s
  1246. T40_ftrim .EQU $0123 ; Fuel trim update timer. Fuel trim are not updated unless this timer is expired (=0). It is set to 5 sec when condition are stable, i.e. fuel trim are update only after conditions are stable for more than 5 sec
  1247. T40_noPower .EQU $0124 ; Timer is init at 0.125sec on every loop when ECU receives power? Will reach 0 when the ECU is about to turn-off (ECU relay turns-off after a few seconds...)
  1248. T40_revving .EQU $0125 ; Timer used in updating iscStStall. Re-init to 0.5sec if tpsRaw < 86% or airVol >= $3a. Timer will start counting when tpsRaw>86% and airVol < $3a and will expire 0.5sec later. Keeps track of rapid throttle plate opening in stalling calculations???
  1249. T40_iscLrn .EQU $0126 ; Timer looping at 40 (produces 1 Hz) used to update isc0/isc1 and iscY1/iscY2/iscY3 at 1Hz (isc learning...)
  1250. T40_stall .EQU $0127 ; Used to update iscStStall at ~2Hz
  1251. T40_acOnTrans .EQU $0128 ; Used to filter out (0.1sec) the impact of A/C being turned-on (transcient load) when evaluating whether the engine is running too slow (<500rpm)
  1252. T40_iscStart .EQU $0129 ; Used to decrement iscStStartMaster as a function of time upon engine startup
  1253. T40_checkTargRpm .EQU $012a ; Timer used to schedule every 1sec the comparison between current rpm to target rpm and adjust isc if necessary
  1254. T40_iSpAdj .EQU $012b ; Timer is 0 when Idle speed adjustment mode is active. Set to 0.2sec after both timing adjustment and ECU test mode terminals are grounded. i.e. idle speed adjustement mode is applied 0.2sec after terminals are grounded...
  1255. T40_21 .EQU $012c ; For E932, used to decrement iscStBaseAcAdj at 2.22Hz
  1256. T40_obdCmd .EQU $012d ; Implement the processing of OBD command code, set to $f0 (6 seconds at 40Hz) if an injector is off or an actuator on
  1257. T40_acOn .EQU $012e ; Implement min time before engaging A/C clutch after A/C button is pressed
  1258. T40_acOnRpm .EQU $012f ; Implement min time before engaging A/C clutch after RPM > 438 (after car is started-up)
  1259. T40_acCut .EQU $0130 ; Implement the 5s A/C cutoff when TPS goes above (and stays above) 82% in AT (5 sec countdown)
  1260. T40_26 .EQU $0131 ; Unused (but decremented...)
  1261. T40_27 .EQU $0132 ; Unused (but decremented...)
  1262.  
  1263.  
  1264. ;-----------------------------------------
  1265. ; Software timer at ~40Hz decremented individually
  1266. ;-----------------------------------------
  1267. T40s_Idle .EQU $0133 ; Only decremented under some specific conditions, init with $1e(0.75s) when idle switch is off, will reach 0 when idle switch has been on for more than 0.75s, used to condition idle flag with A/C switch.
  1268.  
  1269. ;----------------------------------------------------
  1270. ; $134-$13f, series of software timers (counting down to 0)
  1271. ; decremented in subroutine 4 at ~2Hz
  1272. ;----------------------------------------------------
  1273. T2_crank .EQU $0134 ; Set to $ff when startingToCrank, starts counting when engine is no more startingToCrank (engine running or other state...)
  1274. T2_EcuPower .EQU $0135 ; Starts counting from $ff when the ECU receives power, used to blink the "check engine" light when the ECU is turned on.
  1275. T2_closedLp .EQU $0136 ; Used to prolong closed loop mode when we go over an airVolTB threshold for a short period of time, Init to 12sec or 20sec
  1276. T2_o2Sensor .EQU $0137 ; Used to validate the o2 sensor voltage. If timer expires with the o2 sensor voltage never switching (rich/lean) then o2 sensor is not working correctly...
  1277. T2_hotEnrich .EQU $0138 ; Used for fuel enrichement during 120sec after starting engine under very hot intake air temperature (reduce vapor lock???)
  1278. T2_airVolT .EQU $0139 ; Set to 5 sec whenever airVolT>24. Will expire once airVolT<=15 for more than 5sec
  1279. T2_6 .EQU $013a ; Unused? (but decremented...)
  1280. T2_snsrChk .EQU $013b ; Sensor is flagged as bad only when it has been consistently been tested as bad for 4sec. T2_snsrChk implement that 4sec. Initialized to 4 sec everytime sensrChkIdx is reset to 0
  1281. T2_o2Chk .EQU $013c ; Used in o2 sensor testing/validation. re-initialized to 30sec as long as all the testing pre-conditions are not met or as long as we are running rich. Starts counting when we are running lean and pre-conditions are met...
  1282. T2_egrtChk .EQU $013d ; Used in egrt sensor testing/validation.
  1283. T2_stCrank .EQU $013e ; Init to $ff when startingToCrank or when engine just started rotating, starts counting after state change
  1284. T2_11 .EQU $013f ; Unused? (but decremented...)
  1285.  
  1286. ;---------------------------------------------------
  1287. ; 140-144, series of software timers (counting down to 0)
  1288. ; decremented in subroutine 4 at ~0.5Hz
  1289. ;---------------------------------------------------
  1290. T0p5_crank1 .EQU $0140 ; Set to $ff when startingToCrank, starts counting when engine is no more startingToCrank (engine running or other state...)
  1291. T0p5_crCold .EQU $0141 ; Basically not null for 120sec after a cold engine is being cranked/started. Initialized to 120sec when startingToCrank and temperature(ectFiltered) <= 88degC or to 0 if startingToCrank and temperature(ectFiltered) >88degC. Never updated otherwise. Starts counting when we are not startingToCrank. Reset to 0 when notRotating.
  1292. T0p5_purge .EQU $0142 ; Used to implement pulsewidth modulation of the purge solenoid (if some conditions are met), period is very long, 236sec
  1293. T0p5_crank2 .EQU $0143 ; Similar to T0p5_crank1
  1294. T0p5_ect .EQU $0144 ; Reloaded with 5 min on every loop. Starts counting from 5 min only when ect equals exactly 41degC, used in ECT sensor fault routine. Since ect should not stay at that temp for long, counter should never reach 0???
  1295.  
  1296. Tcas .EQU $0145 ;:$0146 ; Tcas (125KHz clock, half the real clock...) is the time(s) per cas interrupt * 125000, rpm = 60/(2*Tcas/125000), Tcas = 60/(2*rpm/125000) (there are 4 cas interrupt for every 2 engine rotations). Tcas is calculated from [TcasLast0:TcasLast1]/2
  1297. TcasOld .EQU $0147 ;:$0148 ; previous value of Tcas
  1298. airDiffPos1 .EQU $0149 ; airDiffPos is transfered to it in subroutine 1
  1299. airDiffNeg1 .EQU $014a ; airDiffNeg is transfered to it in subroutine 1
  1300. mafRaw16 .EQU $014b ;:$014c ; 16 bit mafRaw, Airflow sensor pulse frequency (x/10.24)Hz, calculated from filtered air count (airCnt0:airCnt1) and rpm
  1301. tpsDiffMax2 .EQU $014d ; Value tpsDiffMax1 is transfered here on every main loop execution
  1302. ectCond .EQU $014e ; Conditionned ect for table interpolation, calculated from ectFiltered, see around L1035
  1303. iatCond .EQU $014f ; Condtionned intake air temperature -> validated and offset/clipped = max(min(iatChecked,$e0)-$20,0)
  1304. airVolCond .EQU $0150 ; Conditionned airVol used in table interpolation
  1305. rpmIndex1 .EQU $0151 ; Set to min(max(RPM31p25-500rpm,0),4500rpm), used in 2D interpolation of t_egrDutyFact (column)
  1306. baroCond .EQU $0152 ; Conditionned barometric pressure, non-linear range of $00 to $80 (0.45bar to 0.92bar): 1:1 from $00 to $40 and 2:1 from $40 to $80
  1307. injMasComp .EQU $0153 ;:$0154 ; totMasComp*16 * injComp/128
  1308. totMasComp .EQU $0155 ;:$0156 ; Total mas compensation factor, (masComp+t_masComp(xx))* masLinComp/128
  1309. masLinComp .EQU $0157 ; Interpolated t_masLin, compensate for airflow sensor non-linearity as a function of iat, baro and airflow sensor frequency
  1310. L0158 .EQU $0158 ; Not used????
  1311. openLoopEnr .EQU $0159 ; Open loop enrichement factor, based on timing/knock fuel enrichment conditionned on tps and timer based enrichement
  1312. o2FuelAdj .EQU $015a ; Factor to increase/reduce fuel depending on o2 sensor voltage/feedback, value from 0 to 255, $80=100%->no fuel adjustment. o2FuelAdj = o2Fbk +/- t_closedLpV1(xx) or t_closedLpV2(xx)
  1313. workFtrim .EQU $015b ; Working fuel trim, the fuel trim selected according to current fuel trim range
  1314. coldTempEnr .EQU $015c ; Fuel enrichement factor for cold engine under low airflow conditions, Value of $80=100% means no enrichement
  1315. enrWarmup .EQU $015d ; Current fuel enrichment during warmup/startup, enrichement factor = (2*enrWarmup+$80)/$80
  1316. T_enrWarm .EQU $015e ; Counter used to lower enrWarmup as a function of time down to 0
  1317. iatCompFact .EQU $015f ; Air density factor as a function of temperature ($80=1.0)
  1318. baroFact .EQU $0160 ; Barometric pressure factor, pressure=(baroFact/128)bar
  1319. timFuelEnr .EQU $0161 ; Fuel enrich based on knock/timing??? (Temporarily the timing map value)
  1320. T40s_iscStable .EQU $0162 ; Timer is re-initialized to various values (highest of new and current is kept) every time a ISC impacting load is detected (i.e power steering pump is turned on). Timer is decremented only when iscStepTarg=iscStepCurr, i.e. idle speed target is reached.
  1321. ; It will therefore reach 0 only when the ISC step has reached its target and stayed there for a while, ISC is stable...
  1322. iscStStall .EQU $0163 ; This is the minimum isc step to use when the idle switch transition from off to on. It is decreased by 3 at ~20Hz???. I suppose this is to smooth the rapid change of airflow when the throttle plate closes and reduce the possibility of stalling the engine
  1323. iscStStartUsed .EQU $0164 ; This is the current value of the offset to add to base isc step when the engine was just started. It is slowly decreased (following iscStStartMaster) until the isc step stabilizes. It then stays constant. Whatever value remains after stabilization is used to update iscYn learning variables
  1324. iscLowBatt .EQU $0165 ; Keep track of battery condition for ISC spindle updating. Bit 7 is set when battRaw >= 10V (with hysteresis). 2 lower bits used as counter (3 max) as to how many consecutive times battRaw >= 10V, ISC spindle is not moved until this counter is $03
  1325. iscStTargSpec .EQU $0166 ; The value that will be stored in iscStepTarg when the engine is runnning but iscLrnFlags.1 is set
  1326. iscStBase .EQU $0167 ; Basic ISC step as a function of ECT
  1327. iscStBaseAc .EQU $0168 ; iscStBase corrected for A/C and transmission load
  1328. iscStBaseCSt .EQU $0169 ; iscStBase corrected for cold start period, i.e. high ISC step at start and then decreasing towards iscStBase over 120sec. Set to 0 after 120sec
  1329. iscStBarOff .EQU $016a ; Offset to add to the basic ISC step to compensate for barometric pressure
  1330. iscStBaseAcAdj .EQU $016b ; For E932, used to adjust iscStBaseAc when transmission is engaged, i.e. drive, decremented down to 0 at 2.22Hz...
  1331. idleSpdInit .EQU $016c ; Preliminary idle speed target (xx*7.8125)rpm, t_idleSpd(ect) or t_idleSpdDr(ect), used in the computation of idleSpdTarg
  1332. idleSpdMin .EQU $016d ; Minimum idle speed target (xx*7.8125)rpm, used in the computation of idleSpdTarg
  1333. L016e .EQU $016e ; Unused?
  1334. advTotal .EQU $016f ; Sum of the timing (xx-10)degrees BTDC from the timing maps (timingOct) and of three other timing corrections (advEct-$80, advIat-$80, advRpm-$80)
  1335. timingOct .EQU $0170 ; Base timing (xx-10)degrees corrrected for octane: timingOct = alpha * t_timingHiOct(rpm, load) + (1-alpha) * t_timingLoOct(rpm, load) where alpha = octane/255
  1336. advEct .EQU $0171 ; Ect based timing correction (xx-$80)degrees
  1337. advIat .EQU $0172 ; Iat based timing correction (xx-$80)degrees
  1338. advRpm .EQU $0173 ; Rpm based timing correction (xx-$80)degrees
  1339. coilChkCnt .EQU $0174 ; Used to set an error flag if the ignition coil sensing circuit shows that the ignition is not working properly
  1340. coilHist .EQU $0175 ; coilHist basically contains the ignition coil sensing circuit history (0 or 1 from port4.2) for the last 8 CAS interrupts, bit 7 being the oldest and bit 0 the newest
  1341. T40s_octane .EQU $0176 ; octane timer decremented at 40Hz and looping at $10. octane is updated when timer reaches 0 (at 2.5Hz total)
  1342. knockTimer .EQU $0177 ; Used in the validation of the raw knock sensor voltage received from the ADC
  1343. egrtTimerThr .EQU $0178 ; Timer threshold (compared to T0p5_crank2) used to decide whether enough time has elapsed to test the egrt sensor (180sec or 360sec), threshold is ect based
  1344. sensrChkIdx .EQU $0179 ; The current index in table t_snsrChk indicating which sensor is to be checked/tested next.
  1345. obdCode .EQU $017a ; Contain the latest code received from OBD connector
  1346. errCodeIdx .EQU $017b ; Processing of diagnoctic port error code output (heart beat mode),
  1347. ; c4:c3:c2:c1:c0 is the index of the current error being output
  1348. ; d2:d1:d0 are used as a small 2Hz timer to produce the "heart beat"...
  1349. ; bit 0 (0x01): c0
  1350. ; bit 1 (0x02): c1
  1351. ; bit 2 (0x04): c2
  1352. ; bit 3 (0x08): c3
  1353. ; bit 4 (0x10): c4
  1354. ; bit 5 (0x20): d0
  1355. ; bit 6 (0x40): d1
  1356. ; bit 7 (0x80): d2
  1357. errCodeProc .EQU $017c ; Loaded with the error code (t_snsrChkCode) being output to the test connector (heart beat mode) and then updated as the code is being output.
  1358. ; bit 0 (0x01): a0 a3:a2:a1:a0 is the number of short pulse left to output to connector
  1359. ; bit 1 (0x02): a1
  1360. ; bit 2 (0x04): a2
  1361. ; bit 3 (0x08): a3
  1362. ; bit 4 (0x10): b0 b2:b1:b0 is the number of long pulse left to output to connector
  1363. ; bit 5 (0x20): b1
  1364. ; bit 6 (0x40): b2
  1365. ; bit 7 (0x80): c0 Set to 1 when a new code is loaded, reset to 0 at midpoint between long and short pulses
  1366. egrDuty128 .EQU $017d ; EGR solenoid Duty cycle value from 0 to $80 produces 0 to 100% (not sure of correspondance)
  1367. egrDuty .EQU $017e ; EGR solenoid Duty cycle (48-value)/48, value of table at FF88 interpolated by ECT
  1368. bGaugeODuty .EQU $017f ; Boost gauge "off-duty" cycle, value between $00 and $18, $00 corresponding to the maximum of the boost gauge scale
  1369. T40s_bcs .EQU $0180 ; bcs timer, decremented at 40Hz, loops at $14 (20), bcs duty cycle is updated when this timer reaches 0 (at 2Hz)
  1370. bcsDuty .EQU $0181 ; bcs duty cycle, duty cycle = (48-value)/48
  1371. T40s_tps .EQU $0182 ;
  1372. ectStCrank .EQU $0183 ; Loaded with ectFiltered when engine is startingToCrank, used in ect sensor check
  1373. rpmX4Filt .EQU $0184 ;:$0185 ; Filtered version of 16*rpm4 (xx*16/3.90625)rpm. Filtering is achieved using exponential averaging with alpha = 0.90625
  1374. injCount .EQU $0186 ; Used in the calculation of injPwStart. Incremented by 1 (255 max) every time injPw !=0 in interrupt rountine (fuel is injected)
  1375. airCntMin0 .EQU $0187 ; [airCntMin0:airCntMin1] is the minimum value of [airCntNew0:airCntNew1] before it is used for airCnt0 calcuations
  1376. airCntMin1 .EQU $0188 ; See airCntMin0
  1377.  
  1378.  
  1379. ;------------------------------------------------------------
  1380. ; Unused memory block, except for iscStStartMaster
  1381. ;
  1382. ; Also provides a buffer space in case of stack overflow...
  1383. ; Memories should always be 0 else it means the stack
  1384. ; overflowed in this region...
  1385. ;------------------------------------------------------------
  1386. L0189 .EQU $0189 ;
  1387. L018a .EQU $018a ;
  1388. L018b .EQU $018b ;
  1389. L018c .EQU $018c ;
  1390. iscStStartMaster .EQU $018d ; This is the master isc step offset used upon engine startup. It is initialized with a value from table and then decreased as a function of time down to 0. See iscStStartUsed for more details...
  1391. L018e .EQU $018e ;
  1392. L018f .EQU $018f ;
  1393. L0190 .EQU $0190 ; Memory cleared up to (and including) here
  1394. ramClearEnd .EQU $0190 ;
  1395.  
  1396. ;--------------------------------------------
  1397. ; Memory below is reserved for the stack
  1398. ;--------------------------------------------
  1399. L0191 .EQU $0191 ;
  1400. L0192 .EQU $0192 ;
  1401. L0193 .EQU $0193 ;
  1402. L0194 .EQU $0194 ;
  1403. L0195 .EQU $0195 ;
  1404. L0196 .EQU $0196 ;
  1405. L0197 .EQU $0197 ;
  1406. L0198 .EQU $0198 ;
  1407. L0199 .EQU $0199 ;
  1408. L019a .EQU $019a ;
  1409. L019b .EQU $019b ;
  1410. L019c .EQU $019c ;
  1411. L019d .EQU $019d ;
  1412. L019e .EQU $019e ;
  1413. L019f .EQU $019f ;
  1414. L01a0 .EQU $01a0 ;
  1415. L01a1 .EQU $01a1 ;
  1416. L01a2 .EQU $01a2 ;
  1417. L01a3 .EQU $01a3 ;
  1418. L01a4 .EQU $01a4 ;
  1419. L01a5 .EQU $01a5 ;
  1420. L01a6 .EQU $01a6 ;
  1421. L01a7 .EQU $01a7 ;
  1422. L01a8 .EQU $01a8 ;
  1423. L01a9 .EQU $01a9 ;
  1424. L01aa .EQU $01aa ;
  1425. L01ab .EQU $01ab ;
  1426. L01ac .EQU $01ac ;
  1427. L01ad .EQU $01ad ;
  1428. L01ae .EQU $01ae ;
  1429. L01af .EQU $01af ;
  1430. L01b0 .EQU $01b0 ;
  1431. L01b1 .EQU $01b1 ;
  1432. L01b2 .EQU $01b2 ;
  1433. L01b3 .EQU $01b3 ;
  1434. L01b4 .EQU $01b4 ;
  1435. L01b5 .EQU $01b5 ;
  1436. L01b6 .EQU $01b6 ;
  1437. L01b7 .EQU $01b7 ;
  1438. L01b8 .EQU $01b8 ;
  1439. L01b9 .EQU $01b9 ;
  1440. L01ba .EQU $01ba ;
  1441. L01bb .EQU $01bb ;
  1442. L01bc .EQU $01bc ;
  1443. L01bd .EQU $01bd ;
  1444. L01be .EQU $01be ;
  1445. stack .EQU $01bf ; Top of stack location(grows backward (push-> SP=SP-1)
  1446.  
  1447.  
  1448.  
  1449. ;***************************************************************
  1450. ;*
  1451. ;*
  1452. ;* Unused/Unavailable memory?
  1453. ;*
  1454. ;*
  1455. ;***************************************************************
  1456. empty1 .EQU $01C0 ;:$01FF
  1457.  
  1458.  
  1459.  
  1460. ;******************************************************************
  1461. ;
  1462. ;
  1463. ; 32KB chip address range start
  1464. ;
  1465. ;
  1466. ;******************************************************************
  1467. epromStart .org $8000
  1468.  
  1469.  
  1470.  
  1471. ;******************************************************************
  1472. ;
  1473. ;
  1474. ; Battery gauge code
  1475. ;
  1476. ; 0psi boost ~12.14V ~ 40% duty
  1477. ;
  1478. ;
  1479. ;
  1480. ;******************************************************************
  1481. .fill newCode-$, $ff
  1482. newCode .org $CB00
  1483. #ifdef batteryGauge
  1484. battGauge ldab battRaw ; b=battery voltage
  1485. subb #$8C ; remove 10.262V
  1486. lsrb ;
  1487. lsrb ;
  1488. tba ;
  1489. lsrb ;
  1490. aba ;
  1491. tab ; b = 3/8*(Vbatt-10.262v), gives a effective range of 10.262V to 14.95V (0 to 24 in boost gauge range)
  1492. rts
  1493. #endif
  1494.  
  1495.  
  1496.  
  1497. ;******************************************************************
  1498. ;
  1499. ;
  1500. ; Empty space
  1501. ;
  1502. ;
  1503. ;******************************************************************
  1504. .fill codeStart-$, $ff
  1505.  
  1506.  
  1507.  
  1508. ;******************************************************************
  1509. ;
  1510. ;
  1511. ; Start of code after reset
  1512. ;
  1513. ;
  1514. ;******************************************************************
  1515. codeStart .ORG $ceff-codeOffset
  1516. jmp reset
  1517.  
  1518.  
  1519. ;******************************************************************
  1520. ;
  1521. ;
  1522. ; Empty space
  1523. ;
  1524. ;
  1525. ;******************************************************************
  1526. empty2 .fill obdTable-empty2, $ff
  1527.  
  1528.  
  1529.  
  1530. ;******************************************************************
  1531. ;*
  1532. ;* OBD interface queries, commands
  1533. ;*
  1534. ;* Codes from $00 to $3d: Regular queries, return the value of the
  1535. ;* variables showed in obdTable located below,
  1536. ;* see each variable definition... First value in table
  1537. ;* correspond to obd query code $00, increases by 1
  1538. ;* for each table value
  1539. ;*
  1540. ;* Codes from $3e to $3f: Converted to $3d, see that obd code
  1541. ;*
  1542. ;* Codes from $40 to $c9: Returns what is stored in that ram address
  1543. ;*
  1544. ;* Codes from $ca to $ca:: Erase all fault codes and returns $00 if
  1545. ;* engine not rotating. If engine is rotating, all
  1546. ;* actuators/injector commands are reset and $ff
  1547. ;* is returned.
  1548. ;*
  1549. ;* Codes from $cb to $f0: Returns what is stored in that ram address
  1550. ;*
  1551. ;* Codes from $f1 to $fc: Injector/actuators commands, returns $ff if
  1552. ;* successfull
  1553. ;*
  1554. ;* $f1: Activate boost control solenoid
  1555. ;* $f2: Unused in code
  1556. ;* $f3: Activate egr solemoid
  1557. ;* $f4: Activate fuel pressure solenoid
  1558. ;* $f5: Activate purge solenoid
  1559. ;* $f6: Turn on fuel pump
  1560. ;* $f7: Disable injector #6 (inoperative in code)
  1561. ;* $f8: Disable injector #5 (inoperative in code)
  1562. ;* $f9: Disable injector #4
  1563. ;* $fa: Disable injector #3
  1564. ;* $fb: Disable injector #2
  1565. ;* $fc: Disable injector #1
  1566. ;*
  1567. ;* Codes from $f1 to $ff: Special queries
  1568. ;*
  1569. ;* $fd: Serial link test, returns $b5 (E931) or $b7 (E932)
  1570. ;* $fe: resistor strapping low word from t_strap3
  1571. ;* $ff: resistor strapping high word from t_strap3
  1572. ;*
  1573. ;******************************************************************
  1574. .org $d000-codeOffset
  1575. obdTable .byte port1, port2, port3, port4 ; obd $00 to $03
  1576. .byte port5, port6, timingAdv, ectRaw ; obd $04 to $07
  1577. .byte isc0, iscY0, isc1, iscY1 ; obd $08 to $0b
  1578. .byte ftrim_low, ftrim_mid, ftrim_hi, o2Fbk ; obd $0c to $0f
  1579. .byte ectFiltered, iatChecked, egrtRaw, o2Raw ; obd $10 to $13
  1580. .byte battRaw, baroRaw, iscStepCurr, tpsRaw ; obd $14 to $17
  1581. .byte closedLpFlags, ftrimFlags, mafRaw, ftrim_low ; obd $18 to $1b
  1582. .byte airVol, accEnr, state1, ftrim_low ; obd $1c to $1f
  1583. .byte rpm8, rpm31, port3Snap1, iscLrnFlags ; obd $20 to $23
  1584. .byte idleSpdTarg, iscStepTarg, knockSum, port3Snap0 ; obd $24 to $27
  1585. .byte port4Snap, injPw, injPw+1, enerLen ; obd $28 to $2b
  1586. .byte airCnt0, airCnt1, injFactor, injFactor+1 ; obd $2c to $2f
  1587. .byte iscFlags0, temp1, temp2, temp3 ; obd $30 to $33
  1588. .byte temp4, temp5, o2BadCnt, egrtBadCnt ; obd $34 to $37
  1589. .byte faultHi, faultLo, iatRaw, stFaultHi ; obd $38 to $3b
  1590. .byte stFaultLo, ftrim_low ; obd $3c to $3d
  1591.  
  1592.  
  1593.  
  1594. ;******************************************************************
  1595. ;
  1596. ;
  1597. ; Code executed after reset
  1598. ;
  1599. ;
  1600. ;******************************************************************
  1601. reset lds #stack ; Set the stack pointer
  1602. bsr ecuInit ; Initialization branch
  1603.  
  1604. ;---------------------------------------------
  1605. ; Main ECU loop executed in low priority
  1606. ; (compared to interrupt code). Loop will
  1607. ; execute slower when the computing load
  1608. ; increases...A minimum of 20Hz is monitored
  1609. ; by the COP function?
  1610. ;---------------------------------------------
  1611. L1001 jsr subroutine1 ;
  1612. jsr subroutine2 ;
  1613. jsr subroutine3 ;
  1614. jsr subroutine4 ;
  1615. jmp L1001 ;
  1616.  
  1617.  
  1618.  
  1619. ;******************************************************************
  1620. ;
  1621. ;
  1622. ; Initialization subroutine
  1623. ;
  1624. ;
  1625. ;******************************************************************
  1626. ;------------------------------------------------------------------
  1627. ; Init all outputs (port1, port2 port5 and port6) to known states
  1628. ;------------------------------------------------------------------
  1629. ecuInit ldd #$bf0f ;
  1630. std port1 ; port1 = 1011 1111, port2 = 0000 ffff
  1631. orm port5, #$ff ; port5 = 1111 1111
  1632. andm port6, #$00 ; port6 = 0000 0000
  1633.  
  1634. ;-------------------------------------------------------------
  1635. ; Init port1 through port5 data direction registers
  1636. ; Init real time interrupt frequency
  1637. ; Init L000f, L0017 and L0024 to 0 (never used in the code???)
  1638. ;-------------------------------------------------------------
  1639. jsr initFunc1 ;
  1640. ldd #$1b3d ;
  1641. staa t1_csr ; t1_csr = 0001 1011, enable injectors and cas interrupts, disable injectors, set cas detection edge polarity?
  1642. stab t2_csr ;
  1643. ldd #$5e0a ;
  1644. std t3_csr0 ; t3_csr0 = 0101 1110, t3_csr1 = 0000 1010, both coils not energized
  1645. jsr init_t1_t2 ;
  1646.  
  1647. ;-----------------------------------------------------------
  1648. ; Clear RAM from ramClearStart to ramClearEnd inclusively
  1649. ;-----------------------------------------------------------
  1650. #ifndef noRamReset
  1651. ldy #ramClearStart ;
  1652. clra ;
  1653. clrb ;
  1654. L1003 std $00,y ; Operation does y = y + 2
  1655. cmpy #ramClearEnd+1 ;
  1656. bcs L1003 ;
  1657. #endif
  1658.  
  1659. ;------------------------------------------------
  1660. ; Read all 8 ADC ports values and store in ram
  1661. ;------------------------------------------------
  1662. ldy #ectRaw ;
  1663. ldaa #$08 ; start with port 0 and start bit set ($08)
  1664. L1004 psha ;
  1665. jsr readAdc2 ;
  1666. stab $00,y ; y = y + 1
  1667. pula ;
  1668. inca ;
  1669. cmpa #$10 ;
  1670. bcs L1004 ;
  1671.  
  1672. ;-------------------------------------------------------
  1673. ; Check if all ISC variables are initialized properly
  1674. ; If not then re-initialize ECU from scratch
  1675. ;-------------------------------------------------------
  1676. ldx #$b000 ;
  1677. cpx isc0 ;
  1678. bcs L1005 ; Branch to re-initialize ECU from scratch
  1679. cpx isc1 ;
  1680. bcs L1005 ; Branch to re-initialize ECU from scratch
  1681. ldx #$6c00 ;
  1682. cpx isc0 ;
  1683. bhi L1005 ; Branch to re-initialize ECU from scratch
  1684. cpx isc1 ;
  1685. bhi L1005 ; Branch to re-initialize ECU from scratch
  1686. ldaa iscStepCurr ;
  1687. cmpa #$87 ;
  1688. bhi L1005 ; Branch to re-initialize ECU from scratch
  1689. coma ;
  1690. anda #$7f ;
  1691. cmpa iscStepCom ;
  1692. bne L1005 ; Branch to re-initialize ECU from scratch
  1693.  
  1694. ;-----------------------------------------------------------
  1695. ; All ISC variables look OK
  1696. ; Check if ram control register was erased (loss of power)
  1697. ;-----------------------------------------------------------
  1698. ldab ramControl ;
  1699. #ifdef octaneReset ;
  1700. bmi L1006a ;
  1701. #else
  1702. bmi L1006 ; Branch if ramControl.7 set, i.e. we already did a fresh reset and power was not lost
  1703. #endif
  1704.  
  1705. ;------------------------------------------------------
  1706. ; Perform a fresh reset, i.e. init ECU from scratch
  1707. ;------------------------------------------------------
  1708. ;---------------------------------------
  1709. ; Reset all faults and fault counters
  1710. ;---------------------------------------
  1711. L1005 clra ;
  1712. clrb ;
  1713. std stFaultHi ;
  1714. std faultHi ;
  1715. std o2BadCnt ;
  1716.  
  1717. ;--------------------
  1718. ; Init ISC variables
  1719. ;--------------------
  1720. ldaa #$80 ;
  1721. staa iscFlags0 ; iscFlags0 = $80, isc max calibration is requested
  1722. clra ;
  1723. jsr iscStepComp ; iscStepCurr = $0, iscStepCom = (~$0 & 7F)
  1724. ldd #$8c00 ;
  1725. std isc0 ; isc0 = $8c
  1726. std isc1 ; isc1 = $00
  1727.  
  1728. ;-------------------------------------------------------------
  1729. ; Set isc coil pattern and pattern index to t_iscPattern(0)
  1730. ;-------------------------------------------------------------
  1731. ldab #$04 ;
  1732. stab iscPatrnIdx ; iscPatrnIdx = $04 (lower two bits = 00b)
  1733. orm port5, #$80 ; ISC coil pattern bit 6 and 7 = 10b = t_iscPattern(0)
  1734. andm port5, #$bf ; ISC coil pattern bit 6 and 7 = 10b = t_iscPattern(0)
  1735.  
  1736. ;-------------------------------------------------------------
  1737. ; Init fuel trim to 100% and ftrimCntr to $80
  1738. ;-------------------------------------------------------------
  1739. ldaa #$80
  1740. tab
  1741. std ftrim_low
  1742. std ftrim_hi
  1743.  
  1744. ;------------------------------------
  1745. ; Init octane to max, i.e. good fuel
  1746. ;------------------------------------
  1747. L1006a ldaa #$ff ; Reset octane value to max value (good fuel, no knock)
  1748. staa octane
  1749.  
  1750. ;-------------------------------------------------
  1751. ; Set the ramControl flag bits since
  1752. ; fresh reset steps are (or were already) done
  1753. ;-------------------------------------------------
  1754. L1006 ldaa #$c0 ;
  1755. staa ramControl ;
  1756.  
  1757. ;-----------------------------------------
  1758. ; Init timing/knock variables to defaults
  1759. ;-----------------------------------------
  1760. ldd #$ffa0 ;
  1761. staa TcasLast0 ;
  1762. staa knockTimer ;
  1763. stab tim61Tot0 ;
  1764. ldaa t_enerLen ;
  1765. staa enerLen ;
  1766.  
  1767. ;-----------------------------------------
  1768. ; Init air count variables to default
  1769. ;-----------------------------------------
  1770. ldab t_airCntMax ; b = t_airCntMax(0)
  1771. stab airCntMax ;
  1772. ldaa #$08 ;
  1773. mul ; d = 8 * airCntMax
  1774. std airCnt0 ;
  1775. std oldAirCnt0 ;
  1776.  
  1777. ;-----------------------------------------
  1778. ; Init engine state flags to notRotating
  1779. ;-----------------------------------------
  1780. ldaa #$10 ;
  1781. staa state3 ; engine notRotating
  1782. staa state1 ; engine notRotating
  1783.  
  1784. ;---------------------------------------------
  1785. ; Init cas flags, current cylinder to default
  1786. ;---------------------------------------------
  1787. ldd #$0503 ;
  1788. staa tdcCasFlags ; Why not use std??? I guess it is not obvious taht they are not contiguous just by looking at variable names...
  1789. stab casCylIndex ;
  1790.  
  1791. ;----------------------
  1792. ; More init to default
  1793. ;----------------------
  1794. ldaa #$ff ;
  1795. staa obdInjCmd ; No obd injector command
  1796. staa coilChkCnt ;
  1797. staa T2_EcuPower ;
  1798. staa vss ; speed = 0
  1799.  
  1800. ;-----------------------------------------------------
  1801. ; Init reed switch flag to current reed switch value
  1802. ; and init 40Hz bit to 1
  1803. ;-----------------------------------------------------
  1804. ldaa port1 ;
  1805. anda #$80 ; Keep only reed switch bit
  1806. inca ;
  1807. staa rtiReedFlags ; Store latest Reed switch in bit 7 and set bit 1 for 40Hz based events
  1808.  
  1809. ;---------------------------------------------
  1810. ; More init to default
  1811. ;---------------------------------------------
  1812. ldaa #$06 ; 30ms
  1813. staa T200_cop ;
  1814. staa T200_40Hz ;
  1815. staa L00ca ; Never used in the code??????????????
  1816.  
  1817. ;---------------------------------------------
  1818. ; More init to default
  1819. ;---------------------------------------------
  1820. ldaa tpsRaw ;
  1821. staa oldTps2 ;
  1822. ldaa #$0e ;
  1823. staa T40_baro ;
  1824. ldd #$1450 ;
  1825. staa T40_2hz ;
  1826. stab T40_0p5hz ;
  1827.  
  1828. ;--------------------------------------------------
  1829. ; Reset all iscFlags0 and
  1830. ; If either min or max isc calibration flag was set
  1831. ; set iscStepCurr = 0 and request max calibration
  1832. ; else set flag indicating max calibration is done???
  1833. ;--------------------------------------------------
  1834. ldaa #$40 ; Assume max calibration flag is set
  1835. brclr iscFlags0, #$a0, L1007 ; branch if both max and min calibration flags are clear (1010 0000)
  1836. clra ;
  1837. jsr iscStepComp ; iscStepCurr = $00, iscStepCom = (~$00 & 7F)
  1838. ldaa #$80 ;
  1839. L1007 staa iscFlags0 ; iscFlags0 = $40 or $80
  1840.  
  1841. ;--------------
  1842. ; Init timer
  1843. ;--------------
  1844. ldaa #$05 ;
  1845. staa T40_noPower ;
  1846.  
  1847. ;------------------------------------------
  1848. ; Init TDC and injector testing valriables
  1849. ;------------------------------------------
  1850. ldab #$08 ; b = 0000 1000
  1851. stab tdcCheck ; tdcCheck = 0000 1000
  1852. stab injToTest ; injToTest = 0000 1000
  1853.  
  1854. ;----------------------------------------------------------------------
  1855. ; Load the ECU configuration variables according to resistor strapping
  1856. ;----------------------------------------------------------------------
  1857. jsr loadConfig ;
  1858.  
  1859. ;----------------------
  1860. ; re-enable interrupts
  1861. ;----------------------
  1862. cli ;
  1863.  
  1864. ;-----------------------
  1865. ; More init to defaults
  1866. ;-----------------------
  1867. clrb ;
  1868. stab T40_mas ;
  1869. stab T40_engRot ;
  1870.  
  1871. ;-------------------------------------
  1872. ; Init Tcas and TcasOld to max value
  1873. ; since engine is not rotating
  1874. ;-------------------------------------
  1875. bsr init_Tcas ;
  1876. rts
  1877.  
  1878.  
  1879.  
  1880. ;******************************************************************
  1881. ;
  1882. ; Initialize timer 1 and 2
  1883. ;
  1884. ;
  1885. ;
  1886. ;******************************************************************
  1887. init_t1_t2 orm t1_csr, #$09 ; Deactivate injector 1 and enable injector 1 output compare interrupts
  1888. orm t2_csr, #$3d ; Deactivate injector 3,2,4 and enable injector 3,2,4 output compare interrupts
  1889.  
  1890. ;---------------------------------------------------
  1891. ; Schedule interrupt in 11us for t1 and t2
  1892. ; i.e. Make sure injectors are actually deactivated
  1893. ;---------------------------------------------------
  1894. ldd t1t2_clk
  1895. addd #$000b
  1896. std t1_outCmpWr
  1897. std t2_outCmpWr
  1898. rts
  1899.  
  1900.  
  1901.  
  1902. ;******************************************************************
  1903. ;
  1904. ; Initialize Tcas and TcasOld to $7fff (infinite, not rotating...)
  1905. ;
  1906. ;
  1907. ;
  1908. ;******************************************************************
  1909. init_Tcas ldx #Tcas
  1910. ldd #$7fff
  1911. std $00,x
  1912. std $02,x
  1913. rts
  1914.  
  1915.  
  1916.  
  1917. ;******************************************************************
  1918. ;
  1919. ;
  1920. ; First subroutine
  1921. ;
  1922. ;
  1923. ;******************************************************************
  1924. ;----------------------------------------------
  1925. ; Clear counter state flag and check if 40Hz
  1926. ; flag was set by real time interrupt
  1927. ;----------------------------------------------
  1928. subroutine1 clra ; a=0, used to accumulate various conditions in code below
  1929. brclr rtiReedFlags, #$01, L1013 ; Branch if 40Hz flag is not yet set (flag is set at ~40Hz in RT interrupt)
  1930. andm rtiReedFlags, #$fe ; Reset bit
  1931.  
  1932. ;---------------------------------------------------------------
  1933. ; 40Hz flag is set, process it (code executed 40 times a second,
  1934. ; at the most...)
  1935. ;---------------------------------------------------------------
  1936. ;---------------------------------------------------------------
  1937. ; Decrement all 40Hz timers (min of 0) from $0117 to $0132
  1938. ;---------------------------------------------------------------
  1939. ldx #T40_2hz ;
  1940. ldab #$1c ;
  1941. jsr decTable ;
  1942. inca ; a.0=1, set at 40Hz
  1943.  
  1944. ;------------------------------------------
  1945. ; Update 10Hz flag based on T40_2hz
  1946. ;------------------------------------------
  1947. ldab T40_2hz ;
  1948. bitb #$03 ;
  1949. bne L1011 ; Branch 3 times out of 4???
  1950. oraa #$02 ; a.1=1, set at ~10Hz
  1951.  
  1952. ;----------------------------------------------------------
  1953. ; Check T40_2hz, loops at $14 (20d), which produces 2Hz
  1954. ;----------------------------------------------------------
  1955. L1011 tstb ;
  1956. bne L1012 ; Branch if T40_2hz is not null yet (takes ~0.5sec)
  1957.  
  1958. ;----------------------------------------------------------------------
  1959. ; Decrement all 2Hz timers (min of 0) from $0134 to $13f
  1960. ;----------------------------------------------------------------------
  1961. ldx #T2_crank ;
  1962. ldab #$0c ;
  1963. jsr decTable ;
  1964. oraa #$04 ; a.2=1, set at 2Hz
  1965.  
  1966. ;-----------------
  1967. ; Re-init counter
  1968. ;-----------------
  1969. ldab #$14 ;
  1970. stab T40_2hz ;
  1971. ;
  1972. ;---------------------------------------------------------
  1973. ; Check T40_0p5hz, loops at $50 (80d), which produces 0.5Hz
  1974. ;---------------------------------------------------------
  1975. L1012 ldab T40_0p5hz ;
  1976. bne L1013 ; Branch if T40_0p5hz is not null yet (takes ~2sec)
  1977.  
  1978. ;----------------------------------------------------------------
  1979. ; Decrement all 0.5Hz timers (stop at 0) from $140 to $145
  1980. ;----------------------------------------------------------------
  1981. ldx #T0p5_crank1 ;
  1982. ldab #$05 ;
  1983. jsr decTable ;
  1984. oraa #$08 ; a.3=1
  1985.  
  1986. ;-----------------
  1987. ; Re-init counter
  1988. ;-----------------
  1989. ldab #$50 ;
  1990. stab T40_0p5hz ; Re-init T40_0p5hz with $50 (2sec)
  1991.  
  1992. ;--------------------------------------------------------
  1993. ; At this point, accum. A contains state of counters updated
  1994. ; in the above code, store it in Tclocks
  1995. ;--------------------------------------------------------
  1996. L1013 staa Tclocks
  1997.  
  1998. ;----------------------------------------------------------------------
  1999. ; Re-init T40_crank, T2_crank, T0p5_crank1 to max if startingToCrank
  2000. ;----------------------------------------------------------------------
  2001. ldaa #$ff ; a = $ff
  2002. brclr state1, #$01, L1014 ; Branch if startingToCrank is clear
  2003. staa T40_crank ; Engine startingToCrank, reset a few timers
  2004. staa T2_crank ;
  2005. staa T0p5_crank1 ;
  2006.  
  2007. ;----------------------------------
  2008. ; Re-init T40_start if key is in start
  2009. ;----------------------------------
  2010. L1014 brset port3Snap0, #$40, L1015 ; Branch if key is not in start
  2011. staa T40_start ; Key in start, re-init counter
  2012.  
  2013. ;-------------------------------------------------------------------
  2014. ; Load config1 and config2 memories depending on config resistors
  2015. ;-------------------------------------------------------------------
  2016. L1015 jsr loadConfig
  2017.  
  2018. ;-------------------------------------------------------
  2019. ; Reset counter T200_cop to $0a (on every loop,
  2020. ; will reach 0 only if main loop takes more
  2021. ; than 50ms=10/200Hz, i.e. main loop slower than 20Hz)
  2022. ; Could be used as a COP monitor to reset ECU???
  2023. ;-------------------------------------------------------
  2024. ldaa #$0a ; 50ms or 20Hz
  2025. staa T200_cop ; Re-init counter
  2026. jsr initFunc1 ; Re-init ports and other stuff on every loop???, maybe used in conjunction with T200_cop timer...???
  2027.  
  2028. ;----------------------------------------
  2029. ; Re-init timer 1 and 2 and t3_csr0
  2030. ;----------------------------------------
  2031. sei ;
  2032. andm t1_csr, #$1b ;
  2033. orm t1_csr, #$18 ;
  2034. orm t2_csr, #$18 ;
  2035. andm t3_csr0, #$5e ;
  2036. orm t3_csr0, #$42 ;
  2037. cli
  2038.  
  2039. ;---------------------------------------------------------
  2040. ; Re-init some stuff in case the engine is not rotating
  2041. ;---------------------------------------------------------
  2042. sei ;
  2043. ldab T40_engRot ;
  2044. bne L1016 ; Branch if T40_engRot not expired
  2045. ldd #$0503 ; T40_engRot reached 0, re-init stuff since engine not rotating
  2046. staa tdcCasFlags ;
  2047. stab casCylIndex ; Why not std?
  2048. clr injPw ;
  2049. jsr init_t1_t2 ;
  2050.  
  2051. ;---------------------------------------------------------
  2052. ; Re-init cas related controls if T40s_casInt is expired
  2053. ; i.e. no CAS interrupts received for over 1.275sec
  2054. ;---------------------------------------------------------
  2055. L1016 brset T40s_casInt, #$ff, L1017 ; Branch if T40s_casInt not expired (not 0)
  2056. clr tdcCasCount ; tdcCasCount = 0
  2057. orm t3_csr0, #$0c ; set 0000 1111, disable both power transistor coils and ???
  2058. orm t3_csr1, #$0a ; set 0000 1010,
  2059. clra ;
  2060. staa enerFlags ;
  2061. L1017 cli ;
  2062.  
  2063. ;------------------------------
  2064. ; Check if ECU is in test mode
  2065. ;------------------------------
  2066. brset port4, #$08, L1018 ; Branch if ECU test mode terminal is grounded
  2067.  
  2068. ;--------------------------------------
  2069. ; Not in test mode, Reset serial comm.
  2070. ;--------------------------------------
  2071. ldd sci_scr ; Read serial port at address 0011 (status) and 0012 (data) (clears it)?
  2072. ldd #($0400 |((baudRate & $03)<<8)) ; A=06, B=00
  2073. std sci_baud ; set serial port mode, sci_rate=06, sci_cr=00
  2074. orm obdInjCmd, #$3f ; Reset all injector off commands
  2075. clr obdActCmd ; Reset all actuator on commands
  2076. andm obdFlags, #$3c ; Reset stored serial port state to 00xxxx00?, FA.0 and FA.1 are reset to 0
  2077. bra L1022
  2078.  
  2079. ;------------------------------------
  2080. ; At this point, we are in test mode
  2081. ;------------------------------------
  2082. L1018 brset obdFlags, #$02, L1019 ; Check if port2.4 initialized to 1 (output to serial connector)?
  2083. orm port2, #$10 ; Set output to serial port to 1 (heart beat level on diagnostic port if TE not enabled)
  2084. orm obdFlags, #$02 ; Set $FA.1 indicating we initialized default serial port output
  2085. L1019 brset obdFlags, #$01, L1020 ; branch if FA.0 is 1? (FA.0 seems to be toggled on every loop)
  2086.  
  2087. ;---------------------------------------------------------------------------------
  2088. ; At this point serial tx was previously enabled, reset all parameters anyway
  2089. ; Code is executed only after tx is enabled on first loop (preamble is sent,
  2090. ; we don't want to receive the echo...) and then at 1/2 loop frequency
  2091. ;---------------------------------------------------------------------------------
  2092. ldaa #($04 | (baudRate & $03)) ;
  2093. ldab sci_scr ;
  2094. andb #$fa ;
  2095. orab #$18 ;
  2096. std sci_baud ; Set baud rate and serial port mode
  2097. orm obdFlags, #$01 ;
  2098. bra L1021 ;
  2099. L1020 orm sci_scr, #$02 ;
  2100. andm obdFlags, #$fe ;
  2101. L1021 clr errCodeProc ; Reset code (no code...) being output to test connector (heart beat mode)
  2102.  
  2103. ;------------------------------------------------
  2104. ; Build port3Snap0 from port3
  2105. ;------------------------------------------------
  2106. L1022 ldaa port3 ; a = port3
  2107. anda #$fb ; Reset 0000 0100
  2108. #ifdef E932
  2109. ldab T40_start ;
  2110. addb #$3c ; add 1.5s
  2111. bcc L1023 ; branch if key was out of start for more than than 1.5s
  2112. #endif
  2113. oraa #$20 ; force setting of park/neutral flag
  2114. L1023 ldab T40_crank ;
  2115. addb #$ac ; 4.3s
  2116. bcc L1024 ; branch if engine stopped "startingToCrank" more than 4.3s ago
  2117. oraa #$10 ; Force setting of A/C switch flag
  2118. L1024 brclr state1, #$11, L1025 ; Branch if both notRotating and startingToCrank clear
  2119. oraa #$30 ; engine is either notRotating or startingToCrank, force setting of both A/C switch and park/neutral flags
  2120. L1025 ldab ectFiltered ;
  2121. cmpb #$9b ; 10.2degC
  2122. bcs L1026 ; Branch if ECT temperature lower than threshold
  2123. anda #$f7 ; Reset $08, power steering flag
  2124. L1026 staa port3Snap0 ;
  2125.  
  2126. ;----------------------------
  2127. ; Build port4Snap from port4
  2128. ;----------------------------
  2129. ldaa port4 ;
  2130. anda #$78 ; Only keep 01111000
  2131. staa port4Snap ;
  2132.  
  2133. ;------------------------------------------
  2134. ; Read some ADC inputs
  2135. ; ECT (engine coolant temp)
  2136. ; IAT (intake air temp)
  2137. ; BARO
  2138. ; O2
  2139. ; EGRT
  2140. ;------------------------------------------
  2141. ldy #$00cb ;
  2142. ldaa #$08 ;
  2143. L1027 psha ;
  2144. jsr readAdc1 ;
  2145. cli ;
  2146. stab $00,y ; y = y + 1
  2147. pula ;
  2148. inca ;
  2149. cmpa #$0d ;
  2150. bcs L1027 ;
  2151.  
  2152. ;------------------------------------------------------
  2153. ; Validate and condition raw engine coolant temperature
  2154. ;------------------------------------------------------
  2155. andm state2, #$f0 ; Reset error flags before update below
  2156. ldab ectRaw ; b = ectRaw
  2157. cmpb #$05 ; 141degC
  2158. bcs L1028 ;
  2159. cmpb #$ec ; -52degC
  2160. bls L1029 ;
  2161. L1028 ldab #$1e ; Use default of 83degC
  2162. orm state2, #$01 ; Set error flag
  2163.  
  2164. ;-------------------------------------------------
  2165. ; Check some conditions for filtered ECT update
  2166. ;-------------------------------------------------
  2167. L1029 brclr state2, #$10, L1030 ;
  2168. ldab #$1e ; Use default of 83degC
  2169. L1030 brset state1, #$10, L1033 ; Branch if notRotating
  2170. brclr Tclocks, #$04, L1035 ; Branch if 2Hz signal not set
  2171.  
  2172. ;----------------------------------------------------
  2173. ; At this point 2 Hz signal is set and b = validated ECT
  2174. ;
  2175. ; Filter the validated ECT
  2176. ;
  2177. ; This section of code computes ectFiltered which
  2178. ; is basically the same as validated ECT except
  2179. ; that it can only increase by 3 units every 0.5s...
  2180. ; or decrease by 1 unit every 0.5s
  2181. ;----------------------------------------------------
  2182. ldaa ectFiltered ; a = ectFiltered
  2183. sba ; a = a-b = ectFiltered - validated ECT = ECTdiff
  2184. bcc L1031 ; Branch if ectFiltered >= validated ECT (new temp is higher than old one, which is normal case when warming...)
  2185. ldab ectFiltered ; ectFiltered < validated ECT (temperatured lowered...)
  2186. cmpb #$54 ; 41degC
  2187. beq L1034 ; branch if equal to this temp????
  2188. incb ; else increment validated ECT (decrease temp) by 1 at a time (slowly change it to reflect sensor value...)
  2189. bra L1033 ;
  2190.  
  2191. L1031 cmpa #$03 ; Check ECT difference
  2192. bls L1032 ; Branch if ECT difference <= 3 (5F)
  2193. ldaa #$03 ; Difference higher than 3, use 3
  2194. L1032 suba ectFiltered ;
  2195. nega ; a = ectFiltered-min(ECTdiff,3) = ectFiltered - min(ectFiltered-validatedECT, 3) = validatedECT if difference smaller than 3, else it lags behind...
  2196. tab ; b = ectFiltered-min(ECTdiff,3)
  2197.  
  2198. L1033 ldaa #$96 ; 300s (5 minutes!!!)
  2199. staa T0p5_ect ; Reset counter
  2200. L1034 stab ectFiltered ; ectFiltered = filtered and validated ECT
  2201.  
  2202. ;----------------------------------------------------------
  2203. ; Compute ectCond which is used for table interpolation
  2204. ; Limit max value to $e0 (min temp of -29degC)
  2205. ; Scale by 8 below $20 (temp above 80degC)
  2206. ;
  2207. ; ectFiltered ectFiltered ectCond
  2208. ; -31degC to -59degC $e1-$ff -> $e0
  2209. ; 80degC to -29degC $20-$e0 -> $20-$e0
  2210. ; 81.3degC $1f -> $18
  2211. ; 82.8degC $1e -> $10
  2212. ; 84.3degC $1d -> $08
  2213. ; 158degC to 85.9degC $00-$1c -> $00
  2214. ;----------------------------------------------------------
  2215. L1035 ldab ectFiltered ;
  2216. cmpb #$e0 ; -29degC
  2217. bls L1036 ; Branch if ectFiltered <= $e0
  2218. ldab #$e0 ; Use max of $e0
  2219. L1036 cmpb #$20 ; 80degC
  2220. bcc L1038 ; Branch if ectFiltered >= $20
  2221. subb #$1c ; b = ectFiltered - $1c
  2222. bcc L1037 ; Branch if no underflow
  2223. clrb ; underflow, use 0
  2224. L1037 aslb ;
  2225. aslb ;
  2226. aslb ; b = (ectFiltered-$1c)*8
  2227. L1038 stab ectCond ; Store conditionned ect
  2228.  
  2229. ;-------------------------------------------------
  2230. ; Validate/condition raw intake air temperature
  2231. ;-------------------------------------------------
  2232. ldab iatRaw ;
  2233. cmpb #$0e ; 122degC
  2234. bcs L1039 ; Branch if temp > 122degC
  2235. cmpb #$ea ; -49degC
  2236. bls L1040 ; Branch if temp > -49degC
  2237. L1039 orm state2, #$02 ; Set fault code
  2238. ldab #$7b ; Use 25degC
  2239. L1040 stab iatChecked ;
  2240.  
  2241. ;-----------------------------------------------------------
  2242. ; Compute conditionned IAT for later table interpolation
  2243. ;-----------------------------------------------------------
  2244. ldx #$e020 ; Load x with max/offset (max=$e0)
  2245. jsr clipOffset ; b = max(min(b,$e0)-$20,0)-> offset and clip temp, returns b=$00 to $c0
  2246. stab iatCond ; Conditionned IAT
  2247.  
  2248. ;-------------------------------------------------
  2249. ; Compute air density factor based on air temperature
  2250. ;-------------------------------------------------
  2251. ldx #t_airDens ;
  2252. jsr iatCInterp ;
  2253. stab iatCompFact ; Air density factor
  2254.  
  2255. ;---------------------------------------------------
  2256. ; Check battery voltage for baro sensor validation
  2257. ;---------------------------------------------------
  2258. ldaa battRaw ;
  2259. cmpa #$6d ; 8V
  2260. bcc L1042 ; Branch if more than 8V
  2261. ldaa #$0e ; battery voltage too low, start timer??? (0.35sec)
  2262. staa T40_baro ;
  2263.  
  2264. ;------------------------------------------------------------
  2265. ; Validate baro range, T40_baro is used to ignore baroRaw
  2266. ; When battery<8v (and 0.35s after it is >8V)
  2267. ;------------------------------------------------------------
  2268. L1042 ldab baroRaw ;
  2269. ldaa T40_baro ;
  2270. bne L1044 ; Branch if battery voltage was too low
  2271. cmpb #$e4 ; 1.1 bar
  2272. bcc L1043 ; branch if baroRaw > 1.1
  2273. cmpb #$64 ; .49 bar
  2274. bcc L1045 ; branch if baroRaw > .49
  2275. L1043 orm state2, #$04 ; Set error flag
  2276. L1044 ldab #$cd ; Use 1.0 bar
  2277. L1045 stab baroChecked ;
  2278.  
  2279. ;-----------------------------------------------------
  2280. ; Compute conditionned baro for table interpolation
  2281. ;-----------------------------------------------------
  2282. ldx #$bd5d ;
  2283. jsr clipOffset ; b = max(min(b,$bd)-$5d,0)-> offset and clip baro, returns b = $00 to $60 (0.45bar to 0.92bar???)
  2284. cmpb #$40 ;
  2285. bcs L1046 ; branch if b < $40
  2286. aslb ; else mult by 2
  2287. subb #$40 ; and sub 40 -> 1:1 scale for $00 to $40 and 2:1 scale for $40 to $60, new max is $80, not $60
  2288. L1046 stab baroCond ; Conditionned baro used in table lookup
  2289.  
  2290. ;----------------------------------------------------
  2291. ; Compute barometric pressure factor for fuel inj.
  2292. ;----------------------------------------------------
  2293. ldab baroChecked ;
  2294. ldaa #$a0 ;
  2295. mul ; baroChecked*160
  2296. aslb ;
  2297. adca #$00 ; round-up
  2298. staa baroFact ; barometric pressure factor = rounded baroChecked*160/256 -> pressure is (baroFact/128) bar, i.e. $80 = 1 bar
  2299.  
  2300. ;-------------------------------------------------------------
  2301. ; Transfer tpsDiffMax1 to tpsDiffMax2 and reset tpsDiffMax1
  2302. ;-------------------------------------------------------------
  2303. sei ;
  2304. ldaa tpsDiffMax1 ;
  2305. clr tpsDiffMax1 ;
  2306. cli ;
  2307. staa tpsDiffMax2 ;
  2308.  
  2309. ;-----------------------------------------------------------
  2310. ; If engine is not rotating, re-init Tcas and use rpm = 0
  2311. ;-----------------------------------------------------------
  2312. brclr state1, #$10, L1047 ; Branch if notRotating clear
  2313. jsr init_Tcas ; engine is notRotating, re-init Tcas
  2314. clra ;
  2315. clrb ; use d = rpm = 0 for below
  2316. bra L1048 ;
  2317.  
  2318. ;------------------------------------------------------------------------
  2319. ; Update rpm variables from Tcas (Tcas is obtained from CAS interrupt)
  2320. ;------------------------------------------------------------------------
  2321. L1047 ldd Tcas ;
  2322. jsr calcFreq ; D = $EA600/Tcas = 960000/Tcas = 960000/(125000/2/(rpm/60)) = 0.256*rpm
  2323. L1048 std rpm4 ; RPM4 = #$EA600/Tcas = 0.256*rpm
  2324. jsr scale2m ; scale D by 2
  2325. stab rpm8 ; rpm8 = #$EA600/Tcas/2 = rpm/7.8125
  2326. ldd rpm4 ; D = #$EA600/Tcas
  2327. jsr scale8m ; D = #$EA600/Tcas/8 = rpm/31.25
  2328. stab rpm31 ; rpm31 = #$EA600/Tcas/8 = #$EA600 / (125000/2/(rpm/60)) / 8 = rpm/31.25.
  2329.  
  2330. ;------------------------------------------------------
  2331. ; Compute rpmIndex1 for eventual map interpolation
  2332. ;------------------------------------------------------
  2333. ldaa #$90 ; a=$90 (4500rpm)
  2334. jsr rpmRange ; get rpm for map interpolation, b = min(max(RPM31p25-#$10, 0), $90) = min(max(RPM31p25-500rpm,0),4500rpm)
  2335. stab rpmIndex1 ; rpmIndex1
  2336.  
  2337. ;-------------------------------------------------------------
  2338. ; if notRotating or startingToCrank. Use rpmX4Filt = 16*rpm4
  2339. ;-------------------------------------------------------------
  2340. brclr state1, #$11, L1049 ; branch if notRotating and startingToCrank clear
  2341. ldd rpm4 ; engine is either notRotating or startingToCrank
  2342. asld ;
  2343. asld ;
  2344. asld ;
  2345. asld ; d = 16*rpm4
  2346. andm masCasFlags, #$fe ; reset masCasFlags.1
  2347. bra L1050 ;
  2348.  
  2349. ;-----------------------------------------------------------------
  2350. ; Engine is running, Use rpmX4Filt = filtered(16*rpm4)
  2351. ; Update only when masCasFlags.1 was set by interrupt
  2352. ;
  2353. ; rpmX4Filt is basically the filtered version of rpm4 where
  2354. ; an exponential averaging filter is used
  2355. ;
  2356. ; rpmX4Filt = $e8/256 * oldrpmX4Filt + $18/256 * 16*rpm4
  2357. ; alpha * oldrpmX4Filt + (1-alpha) * 16*rpm4
  2358. ;
  2359. ; where alpha = 0.90625
  2360. ;-----------------------------------------------------------------
  2361. L1049 brclr masCasFlags, #$01, L1051 ; Branch if flag not set
  2362. andm masCasFlags, #$fe ; Reset the flag
  2363. ldx rpmX4Filt ; x = rpmX4Filt
  2364. ldab #$e8 ; b = $e8
  2365. jsr mul816b ; d = $e8/256 * rpmX4Filt, temp3 = lower 8 bits of result
  2366. std rpmX4Filt ; rpmX4Filt = $e8/256 * rpmX4Filt
  2367. ldaa temp3 ;
  2368. staa temp4 ; temp4 = temp3 = lower 8 bits of ($e8/256 * old rpmX4Filt)
  2369. ldd rpm4 ;
  2370. asld ;
  2371. asld ;
  2372. asld ;
  2373. asld ; d = 16 * rpm4
  2374. xgdx ; x = 16 * rpm4
  2375. ldab #$e8 ; b = $e8
  2376. negb ; b = -$e8 = $18 (why not load it directly, maybe mitsu compiler stuff...?)
  2377. jsr mul816b ; d = $18/256 * 16 * rpm4
  2378. xgdx ; x = $18/256 * 16 * rpm4
  2379. clrb ; b = 0
  2380. ldaa temp3 ; a = lower 8 bits of ($18/256 * 16 * rpm4)
  2381. adda temp4 ; a = lower 8 bits of ($18/256 * 16 * rpm4) + lower 8 bits of ($e8/256 * old rpmX4Filt)
  2382. rolb ; b = carry bit (if a carry was generated) from that addition
  2383. rola ; a = a*2 (shift upper bit for roundoff purposes)
  2384. adcb #$00 ; Round off. At this point, b contains the rounded-up highest bit of the addition of the lowest 8 bits
  2385. abx ; x = $18/256 * 16 * rpm4 + rounded lower 1 bit
  2386. xgdx ; d = $18/256 * 16 * rpm4 + rounded lower 1 bit
  2387. addd rpmX4Filt ; d = $18/256 * 16 * rpm4 + $e8/256 * old rpmX4Filt
  2388. L1050 std rpmX4Filt ; Store new value
  2389.  
  2390. ;-------------------------------------------------------------
  2391. ; Restart T40_mas if engine notRotating or startingToCrank
  2392. ;-------------------------------------------------------------
  2393. L1051 brclr state1, #$11, L1052 ; branch if notRotating and startingToCrank clear
  2394. ldaa #$0c ; Engine is either notRotating or startingToCrank
  2395. staa T40_mas ; Restart timer at 0.3s
  2396.  
  2397. ;-----------------------------------------
  2398. ; Set state2 mas flag if T40_mas expired
  2399. ;-----------------------------------------
  2400. L1052 ldaa T40_mas ;
  2401. bne L1053 ; Branch if counter not yet 0
  2402. orm state2, #$08 ; Set bit indicating timer expired
  2403.  
  2404. ;---------------------------------------------------------------------------
  2405. ; Compute mafRaw16 and mafRaw from airCnt0:airCnt1
  2406. ;
  2407. ; Since airCnt0:airCnt1 is filtered airCntNew0:airCntNew1, we have
  2408. ;
  2409. ; mafRaw = $200d * airCnt0 / Tcas / 64
  2410. ; = 8205 * airCntNew0 / Tcas / 64
  2411. ; = 8205 * (N+r) * $9c / Tcas / 64 (see airCntNew0 definition)
  2412. ; = 8205 * (N+r) * $9c / (125000*TcasInSeconds) / 64
  2413. ; = (N+r)/TcasInSeconds/6.25
  2414. ; = "number of airflow sensor pulse per sec" / 6.25
  2415. ;
  2416. ; Where:
  2417. ;
  2418. ; -Tcas is the time required for 1 cas interrupt (there are 4 cas
  2419. ; interrupts for every 2 rotations which basically means 1 cas
  2420. ; interrupt for every complete cycle of one cylinder)
  2421. ; -(N+r) is the number of air sensor pulses received during
  2422. ; one cas interrupt, r<1 is the fractional part. See
  2423. ; the mas interrupt for assumptions...
  2424. ; -Tcas is the cas interrupt period measured at 125KHz
  2425. ;
  2426. ;---------------------------------------------------------------------------
  2427. L1053 ldd Tcas ; d = Tcas
  2428. std temp4 ;
  2429. ldd #$200d ; d = $200d = 8205d
  2430. ldx airCnt0 ;
  2431. stx temp6 ;
  2432. jsr mul1616 ; d = d * temp6:temp7 = ($200d * airCnt0)/65536 = 0.125198*airCnt0
  2433. jsr div3216 ; d = 65536*0.125198*airCnt0/Tcas = 8205 * airCnt0/Tcas
  2434. std mafRaw16 ; 16 bit mafRaw
  2435. #ifdef masLog2X ;
  2436. jsr scale128m ;
  2437. #else ;
  2438. jsr scale64m ; d = 8205*airCnt0/Tcas/64, result is in B...
  2439. #endif
  2440. brset state1, #$10, L1054 ; Branch if engine notRotating
  2441. brclr state2, #$08, L1055 ; Branch if pulse accumulator interrupts received
  2442. L1054 clrb ; No interrupts or notRotating, use 0 air flow
  2443. L1055 stab mafRaw ; Store 8 bit mafRaw = 8205*airCnt0/Tcas/64
  2444.  
  2445. ;----------------------------------------------------------------------
  2446. ; Compute airCntMax (max air count as a function of rpm, ect and iat)
  2447. ;----------------------------------------------------------------------
  2448. ldd rpm4 ; d = rpm4
  2449. cmpa #$03 ; compare high part to 3 -> compare D to 768
  2450. bcs L1057 ; Branch if RPM < 3000
  2451. ldd #$0300 ; RPM >=3000 -> use 3000
  2452. L1057 asld ;
  2453. asld ; scale rpm
  2454. ldx #t_airCntMax ;
  2455. jsr interp1 ; b = t_airCntMax[rpm]
  2456. clra ;
  2457. std temp6 ; temp6:temp7 = t_airCntMax[rpm]
  2458. ldx #L1990 ;
  2459. ldaa ectCond ;
  2460. jsr interp32mul ; D = t_airCntMax[rpm] * L1990[ectCond]
  2461. ldx #L1991 ;
  2462. ldaa iatCond ;
  2463. jsr interp32mul ; D = t_airCntMax[rpm] * L1990[ectCond] * L1991[iatCond]
  2464. jsr ovfCheck ; Check for overflow
  2465. stab airCntMax ; airCntMax = t_airCntMax[rpm] * L1990[ectCond] * L1991[iatCond]
  2466.  
  2467. ;------------------------------------------------
  2468. ; Store airCntMax in airCnt0 and oldAirCnt0
  2469. ; when engine is notRotating or startingToCrank
  2470. ;------------------------------------------------
  2471. brclr state1, #$11, L1060 ; branch if notRotating and startingToCrank clear
  2472. clra ; engine is either notRotating or startingToCrank
  2473. asld ;
  2474. asld ;
  2475. asld ; d = airCntMax*8
  2476. std airCnt0 ; airCnt0:airCnt1 = airCntMax*8
  2477. std oldAirCnt0 ; oldAirCnt0 = airCntMax*8
  2478.  
  2479. ;----------------------------------------------
  2480. ; Compute airCntDef, default airCnt0 value
  2481. ; when no mas interrupts are being received
  2482. ;----------------------------------------------
  2483. L1060 ldy #L2053 ;
  2484. jsr rpmPwise ; b = F(rpm4), see L2053 table
  2485. pshb ;
  2486. ldx #L2036 ;
  2487. jsr interpEct ;
  2488. addb tpsRaw ; b = tpsRaw + L2036[ect]
  2489. bcc L1062 ; overflow check
  2490. ldab #$ff ; Use max
  2491. L1062 ldx #$ba1a ;
  2492. jsr clipOffset ; offset and clip b, b = max(min(tpsRaw + L2036[ect],$ba)-$1a,0)
  2493. lsrb ; b = b/2
  2494. pula ; a = F(rpm4)
  2495. ldx #L1986 ;
  2496. ldy #$0500 ;
  2497. jsr lookup2D2 ; b = L1986[a,b], 2D interpolated air count since stored in airCntDef which is stored in airCnt0 under some cases???
  2498. ldaa #$57 ;
  2499. mul ; b = $57*L1986[a,b]
  2500. jsr scale128m ; b = $57*L1986[a,b]/128
  2501. stab airCntDef ; airCntDef = $57*L1986[a,b]/128
  2502.  
  2503. ;------------------------------------------------------
  2504. ; Re-init airFiltFact (airflow filtering factor)
  2505. ;
  2506. ; Filtering factor depends on current conditions???
  2507. ;------------------------------------------------------
  2508. #ifdef E931
  2509. ldaa #$b3 ; Value to use if timer T40_ftrim2 is expired
  2510. ldab T40_ftrim2 ;
  2511. beq L1064 ;
  2512. #endif
  2513. ldaa #$d1 ;
  2514. brset iscLrnFlags, #$10, L1064 ; Branch if conditions are good to update isc variables
  2515. ldaa #$e4 ;
  2516. L1064 staa airFiltFact
  2517.  
  2518. ;-------------------------------------------------------------
  2519. ; Transfer airDiffPos:airDiffNeg to airDiffPos1:airDiffNeg1
  2520. ; and re-init airDiffPos:airDiffNeg to 0
  2521. ;-------------------------------------------------------------
  2522. clra ;
  2523. clrb ;
  2524. sei ;
  2525. ldx airDiffPos ;
  2526. std airDiffPos ;
  2527. cli ;
  2528. stx airDiffPos1 ;
  2529.  
  2530. ;-----------------------------------------------------------------------------------------
  2531. ; Compute airCntMin0 (minimum value of airCntNew0 before it is used for airCnt0 calc)
  2532. ;-----------------------------------------------------------------------------------------
  2533. clra ;
  2534. clrb ; d = 0
  2535. brset state1, #$11, L1065 ; branch if engine notRotating or startingToCrank (use minimum of 0)
  2536. ldaa #$10 ;
  2537. #ifdef E931
  2538. ldab #$81
  2539. #else
  2540. ldab #$91
  2541. #endif
  2542. mul ;
  2543. ldx Tcas ;
  2544. stx temp6 ;
  2545. jsr mul1616 ;
  2546. ldx #$4000 ;
  2547. stx temp6 ;
  2548. jsr mul1616 ;
  2549. xgdx ;
  2550. #ifdef E931
  2551. ldab baroFact ;
  2552. #else
  2553. ldab #$80 ; Use 1.0 bar
  2554. nop
  2555. #endif
  2556. ldaa iatCompFact ;
  2557. mul ;
  2558. std temp4 ;
  2559. xgdx ;
  2560. jsr div3216 ;
  2561. L1065 std airCntMin0 ; Store "min" used in air count calc
  2562.  
  2563. ;---------------------------------------------------------------
  2564. ; Compute airVol16 and airVol from [airCnt0:airCnt1]*masScalar
  2565. ;---------------------------------------------------------------
  2566. clra ;
  2567. clrb ;
  2568. brset state1, #$10, L1066 ; Branch if engine notRotating
  2569. ldd #masScalar ; 16 bit MAS scalar ($5e86 for 1G, $7A03 for 2g), seem to correspond to (masComp+t_mascomp(72Hz))/512*65536
  2570. std temp6 ; Store for 16 multi.
  2571. ldd airCnt0 ; MAS air count
  2572. jsr mul1616 ; d = masScalar/65536 * [airCnt0:airCnt1]
  2573. L1066 std temp6 ; temp6:temp7 = masScalar/65536 * [airCnt0:airCnt1]
  2574. std airVol16 ; airVol16 = masScalar/65536 * [airCnt0:airCnt1]
  2575. jsr scale2m ; b = masScalar/65536 * airCnt0 / 2 with overflow check
  2576. stab airVol ; airVol = masScalar/65536*[airCnt0:airCnt1]/2 (8 bit airflow)
  2577.  
  2578. ;--------------------
  2579. ; Compute airVolCond
  2580. ;--------------------
  2581. tba ; a = airVol
  2582. jsr L1647 ; b = Apply offset and scaling to airVol???
  2583. stab airVolCond ;
  2584.  
  2585. ;----------------------------------------
  2586. ; At this point [temp6:temp7] = airVol16
  2587. ;
  2588. ; Compute airVolT, airVolTB and airVolB
  2589. ;----------------------------------------
  2590. ldab iatCompFact ;
  2591. jsr mul816_256 ; b = airVol16/2 * iatCompFact/128; [temp6:temp7] = airVol16 * iatCompFact/128
  2592. stab airVolT ; airVolT = airVol16/2 * iatCompFact/128
  2593. jsr mul816_baro ;
  2594. stab airVolTB ; airVolTB = airVol16/2 * iatCompFact/128 * baroFact/128;
  2595. ldd airVol16 ; d = airVol16
  2596. std temp6 ;
  2597. jsr mul816_baro ;
  2598. stab airVolB ; airVolB = airVol16/2 * baroFact/128
  2599.  
  2600. #ifdef E931
  2601. ;---------------------------------------------------
  2602. ; Set ftrimFlags.3 if speed exceed threshold (with
  2603. ; hysteresis) and port3.0 is set?????????
  2604. ;---------------------------------------------------
  2605. andm ftrimFlags, #$f7 ; Assume we reset flag $08, updated below
  2606. ldaa #$18 ; speed threshold = $18
  2607. brclr ftrimFlags, #$04, Md4d4 ; branch is flag not yet set
  2608. ldaa #$1c ; Flag already set, use higher threshold (lower speed threshold)
  2609. Md4d4 andm ftrimFlags, #$fb ; Assume we reset $04
  2610. cmpa vss ;
  2611. bcs L1067 ; Bail if speed < 24km/h (vss=1/speed...)
  2612. orm ftrimFlags, #$04 ; speed > 24km/h, set "threshold exceeded" bit
  2613. ldaa port3Snap0 ; Get stored port3
  2614. anda port3 ; Confirm bit is still set with current value
  2615. lsra ; Get confirmed bit 0 in carry
  2616. bcc L1067 ; Branch if bit was not set
  2617. orm ftrimFlags, #$08 ; Bit was still set, set flag bit
  2618. #endif
  2619.  
  2620. ;-----------------------------------------------
  2621. ; Set ftrimFlags.4 if rpm exceeds
  2622. ; threshold (around 1000rpm), with hysteresis
  2623. ;-----------------------------------------------
  2624. L1067 ldx #L1983 ; x points to initial threshold
  2625. brclr ftrimFlags, #$10, L1068 ; Branch if flag not yet set
  2626. inx ; Flag is set, go to next value (hysteresis)
  2627. L1068 .equ $
  2628. #ifdef E932
  2629. brset port3Snap0, #$20, L1069 ; branch if Park/neutral
  2630. inx ; even more threshold hysteresis...
  2631. inx ; even more threshold hysteresis...
  2632. #endif
  2633. L1069 ldaa rpm31 ;
  2634. andm ftrimFlags, #$ef ; Assume we reset $10
  2635. cmpa $00,x ; Compare rpm to treshold
  2636. bcs L1070 ; branch if rpm31 < L1983(flags...)
  2637. orm ftrimFlags, #$10 ; set flag indicating we are above rpm threshold
  2638.  
  2639. ;----------------------------------------------------------------------------------------
  2640. ; Update the fuel trim range (low, mid, high) according to mafRaw16. Table
  2641. ; t_ftrimRg provides the 2 thresholds with some hysteresis (+/-6Hz)
  2642. ; The trim range is stored in L00e3.0.1 (lowest 2 bits)
  2643. ;
  2644. ; old L00e3.0.1 resulting X new L00e3.0.1
  2645. ; 00 t_ftrimRg 00 if maf < t_ftrimRg(00) otherwise 01
  2646. ; 01 t_ftrimRg 00 if maf < t_ftrimRg(01) otherwise 01 if maf < t_ftrimRg(02) otherwise 10
  2647. ; 10 t_ftrimRg+1 01 if maf < t_ftrimRg(03) otherwise 10
  2648. ; 11 t_ftrimRg+1 01 if maf < t_ftrimRg(03) otherwise 10
  2649. ;
  2650. ; L00e3.0.1 meaning
  2651. ; 00 low trim (below first threshold)
  2652. ; 01 mid trim (between first and second threshold)
  2653. ; 10 high trim (above second threshold)
  2654. ; 11 Never used I think
  2655. ;----------------------------------------------------------------------------------------
  2656. L1070 ldx #t_ftrimRg ; X pointx to t_ftrimRg min1
  2657. ldd mafRaw16 ; d = mafRaw16
  2658. jsr scale64m ; d = mafRaw16/64 (thats equal to mafRaw...?)
  2659. tba ; a = mafRaw16/64 = mafRaw (6.25x)Hz
  2660. clrb ; b=0
  2661. brclr ftrimFlags, #$03, L1074 ;
  2662. brclr ftrimFlags, #$02, L1071 ;
  2663. inx ; X pointx to t_ftrimRg+1
  2664. bra L1073 ;
  2665. L1071 cmpa $01,x ;
  2666. bcs L1077 ; Branch if mafRaw16/64 < t_ftrimRg
  2667. L1073 cmpa $02,x ;
  2668. bcs L1076 ; Branch if mafRaw16/64 < t_ftrimRg
  2669. bra L1075 ;
  2670. L1074 cmpa $00,x ;
  2671. bcs L1077 ; Branch if mafRaw16/64 < t_ftrimRg
  2672. bra L1076 ;
  2673. L1075 incb ;
  2674. L1076 incb
  2675. L1077 ldaa ftrimFlags ; a = ftrimFlags
  2676. anda #$fc ; Reset trim range
  2677. aba ; Add new trim range
  2678. staa ftrimFlags ; Update ftrimFlags
  2679.  
  2680. ;---------------------------------------------------
  2681. ; Restart timer T40_ftrim2 on E931 if
  2682. ;
  2683. ; rpm > 1953rpm
  2684. ; or speed > 15 km/h
  2685. ; or speed < 2.5 km/h
  2686. ; or vss*rpm/15.625 < $cd8 (note speed ~ 1/vss)
  2687. ; or airVol > $38
  2688. ; or tpsDiffMax2 > $04
  2689. ;---------------------------------------------------
  2690. #ifdef E931
  2691. ldd rpm4 ;
  2692. jsr scale4m ;
  2693. cmpb #$7d ; 1953rpm
  2694. bcc Md551 ; Branch if rpm>1953rpm
  2695. ldaa vss ;
  2696. cmpa #$26 ; ~15km/h
  2697. bcs Md551 ; Branch if speed>15km/h
  2698. cmpa #$e2 ; ~2.5km/h
  2699. bcc Md551 ; Branch if speed<2.5km/h
  2700. mul ;
  2701. cmpd #$0cd8 ; d = vss*rpm/15.625
  2702. bcs Md551 ; branch if vss*rpm/15.625 < $cd8
  2703. ldaa airVol ;
  2704. cmpa #$38 ;
  2705. bcc Md551 ; Branch if airVol > $38
  2706. ldaa tpsDiffMax2 ;
  2707. cmpa #$04 ;
  2708. bcs L1078 ; Branch if tpsDiffMax2 < $04
  2709. Md551 ldaa #$78 ; 3 sec
  2710. staa T40_ftrim2 ;
  2711. #endif
  2712.  
  2713. ;---------------------------------------------------
  2714. ; Reload T40s_Idle as long as idle switch is off
  2715. ;---------------------------------------------------
  2716. L1078 brset port3Snap0, #$80, L1079 ; Branch if idle position switch on
  2717. ldaa #$1e ;
  2718. staa T40s_Idle ; Reload down counter (~0.75sec)
  2719.  
  2720. ;---------------------------------------------------------------------
  2721. ; Check for airVolT threshold with hysteresis and update T2_airVolT
  2722. ; ftrimFlags is set when airVolT > 24 and is reset when airVolT <= 15
  2723. ;---------------------------------------------------------------------
  2724. L1079 ldaa #$0f ; Threshold min
  2725. brset ftrimFlags, #$80, L1080 ;
  2726. ldaa #$18 ; Threshold max
  2727. L1080 andm ftrimFlags, #$7f ;
  2728. cmpa airVolT ; Compare current air volume
  2729. bcc L1081 ; Branch if airVolT <= threshold
  2730. orm ftrimFlags, #$80 ; airVolT > threshold, set bit
  2731. ldaa #$0a ; reset timer to 5 seconds
  2732. staa T2_airVolT ;
  2733. L1081 brset state2, #$08, L1083 ; Branch if no pulse accumulator interrupts received
  2734. brset state1, #$10, L1083 ; Branch if engine notRotating
  2735.  
  2736. ;-----------------------------------------------------------
  2737. ; Compute air volume used in fuel cut comparison
  2738. ; it uses 16 bits since 8 bit air volume saturate at ~1g of air/cas
  2739. ;-----------------------------------------------------------
  2740. ldd airVol16 ; d = airVol16
  2741. jsr scale4m ; b = airVol16/4 (makes sure it fits in b only...)
  2742. #ifdef extLoadRange ;
  2743. stab L0054 ;
  2744. #else ;
  2745. stab temp1 ; temp1 = airVol16/4
  2746. #endif ;
  2747. ldaa iatCompFact ; Correct for air temp
  2748. mul ;
  2749. jsr scale128m ; d = airVol16/4 * iatCompFact/128
  2750. ldaa baroFact ; a = baroFact
  2751. mul ;
  2752. jsr scale128m ; d = airVol16/4 * iatCompFact/128 * baroFact/128 (fits in b only)
  2753.  
  2754. ;---------------------------------------------------------------------------------------
  2755. ; Keep the minimum of airVol16/4 and airVol16/4 * iatCompFact/128 * baroFact/128
  2756. ;---------------------------------------------------------------------------------------
  2757. #ifdef extLoadRange
  2758. cmpb L0054 ;
  2759. bcs L1082 ;
  2760. ldab L0054 ;
  2761. #else
  2762. cmpb temp1 ;
  2763. bcs L1082 ; Branch if airVol16/4 * iatCompFact/128 * baroFact/128 <= airVol16/4
  2764. ldab temp1 ; Use max of airVol16/4
  2765. #endif
  2766.  
  2767. ;------------------------------------------------------------------------------
  2768. ; Check air volume for eventual fuel cut
  2769. ; When air volume exceeds a threshold, Timer T40_fuelCut
  2770. ; is not re-initialized on every loop (to 1s) and therefore starts
  2771. ; counting down. when it reaches 0, fuel cut is applied, see L1090 below
  2772. ;------------------------------------------------------------------------------
  2773. L1082 cmpb #fuelCutVal ; Air volume based fuel cut value $a0 = 1.25g/cas
  2774. #ifdef noFuelCut
  2775. brn L1084 ;
  2776. #else
  2777. bcc L1084 ; Branch if air volume>=threshold
  2778. #endif
  2779. L1083 ldab #$28 ; 1 sec
  2780. stab T40_fuelCut ; Re-init counter to 1 sec (Apply fuel cut only after threshold is exceeded for more than 1s)
  2781.  
  2782. ;-----------------------------------------------------------------
  2783. ; Section to update the state1 flags from various conditions
  2784. ;
  2785. ; Bits in b are used to accumulate various loads and states
  2786. ; In this section, b is only set in case we have
  2787. ; to bail to the state1 flag setting section
  2788. ;-----------------------------------------------------------------
  2789. L1084 ldab #$30 ; Starting "state1" value, b=00110000 (not rotating)
  2790. brclr state1, #$20, L1085 ; Bail if this is the first time we compute state1??
  2791. ldaa T40_engRot ;
  2792. beq L1085 ; Bail if engine not rotating
  2793. ldaa T40_noPower ;
  2794. bne L1086 ; Don't bail if timer not expired???
  2795. L1085 jmp L1100 ; bail
  2796.  
  2797. ;----------------------------------------------------
  2798. ; Engine rotating, check if key is in "start"
  2799. ; In this section, b is only set in case we have
  2800. ; to bail to the state1 flag setting section
  2801. ;----------------------------------------------------
  2802. L1086 ldab #$21 ; pre-load new state1 in case we have to bail, b=00100001 (startingToCrank)
  2803. brset port3Snap0, #$40, L1089 ; branch to next state if key not in start???
  2804.  
  2805. ;----------------------------------------------------
  2806. ; Key is in "start", check if rpm is higher than
  2807. ; threshold (engine running?)
  2808. ;----------------------------------------------------
  2809. ldaa #$0e ; starting rpm value (RPM/31.25) $0e = 437.25
  2810. brset state1, #$01, L1087 ; Branch if engine rotating bit was previously set
  2811. ldaa #$0b ; $0b = 343.75RPM
  2812. L1087 brclr injFlags0, #$20, L1088 ; Branch if temperature(ectFiltered) >= -8degC
  2813. adda #$02 ; temperature(ectFiltered) < -8degC, add 62.5 RPM
  2814. L1088 cmpa rpm31 ; compare threshold to current engine speed
  2815. bhi L1085 ; Bail if engine rpm lower than calculated value
  2816.  
  2817. ;---------------------------------------------------------------------
  2818. ; At this point,
  2819. ; key is in "start" or engine rpm is higher than minimum threshold,
  2820. ; minimum conditions are therefore met for the engine to start or be started?
  2821. ;
  2822. ; Use this state to check if we should get fuel injection. If we get stuck in
  2823. ; this state it means engine is rotating but something is wrong...
  2824. ;---------------------------------------------------------------------
  2825. ;---------------------------------------------------------------------
  2826. ; If enough time has elapsed, check if CAS is working normally
  2827. ;---------------------------------------------------------------------
  2828. L1089 ldab #$28 ; pre-load new state1 in case we have to bail, b=00101000 (trying to start but something is wrong...)
  2829. ldaa T40_start ;
  2830. adda #$50 ; add 2s
  2831. bcs L1090 ; Branch if key was out of start for less than 2s (when engine was upgraded from startingToCrank),
  2832. brclr faultHi, #$80, L1090 ; Its been more than ~2s since key was out of start, ECU has had enough time to check if CAS was working, check it, branch if no fault on CAS
  2833. ldaa tdcCasCount ; Fault code set...
  2834. cmpa #$04 ;
  2835. bcs L1085 ; Bail if tdcCasCount<4, this should not have happened at this time since engine has been rotating for a while
  2836.  
  2837. ;----------------------------------------------------------------------
  2838. ; Bail if fuel cut is active (T40_fuelCut=0)
  2839. ;----------------------------------------------------------------------
  2840. L1090 ldaa T40_fuelCut ; Fuel cut timer
  2841. beq L1085 ; Bail if timer 0 (fuel cut is active)
  2842.  
  2843. ;-----------------------------------------------
  2844. ; Bail if ECU is about to be shutoff
  2845. ;-----------------------------------------------
  2846. brset port3, #$02, L1085 ; Bail if IG1 at 0V, ECU is about to turn off after delay...?
  2847.  
  2848. ;-------------------------------------------------------------------------
  2849. ; At this point, minimum conditions are met for the engine
  2850. ; to start or run (rpm>thresh or start switch on), CAS is working,
  2851. ; there is no fuel cut and the ECU is not being turned off
  2852. ;
  2853. ; Basically we know that we should be injecting fuel, do a little more check below...
  2854. ;
  2855. ;
  2856. ; Calculate a maximum rpm that we should have based on maxRpm = baseRpm + rpmOffset
  2857. ; where rpmOffset is additional loads that we calculate below
  2858. ;
  2859. ; Below, a will contain baseRpm and b will be used to accumulate the additional loads as flags...
  2860. ;-------------------------------------------------------------------------
  2861. ;-----------------------------------------------
  2862. ; Get Initial RPM from ECT interpolated table
  2863. ;-----------------------------------------------
  2864. ldx #t_rpmEct ;
  2865. jsr interpEct ;
  2866. tba ; a = initial rpm idle speed, will be changed below (L1095)
  2867. clrb ; b = 00000000, no additionnal loads yet
  2868.  
  2869. ;-----------------------------------------------------------------
  2870. ; Check if T40s_Idle timer expired
  2871. ; (when idle position switch has been on for more than 0.75s)
  2872. ;-----------------------------------------------------------------
  2873. tst T40s_Idle ;
  2874. beq L1091 ; Branch if T40s_Idle already at zero (idle position switch on for more than 0.75s)
  2875.  
  2876. ;----------------------------------------------------------------
  2877. ; Timer not expired, decrement it at 40Hz
  2878. ;----------------------------------------------------------------
  2879. brclr Tclocks, #$01, L1091 ; Branch if basic 40Hz signal not set
  2880. dec T40s_Idle ; Decrement timer
  2881. brset port3Snap0, #$20, L1091 ; Ignore timer if in Park or Neutral (no transmission load)
  2882. beq L1092 ; Branch if T40s_Idle reached 0 this time
  2883.  
  2884. ;-------------------------
  2885. ; Add a load when ???
  2886. ;-------------------------
  2887. L1091 brclr state1, #$04, L1093 ; Branch if state1.2 (idle too fast) was not previously set
  2888. L1092 incb ; Add load
  2889. L1093 .equ $
  2890.  
  2891. ;--------------------------
  2892. ; Add "transmission" load
  2893. ;--------------------------
  2894. #ifdef E932
  2895. brset port3Snap0, #$20, L1094 ; Branch if in Park or Neutral
  2896. addb #$02 ; Set flag indicating "transmission load"
  2897. #endif
  2898. ;--------------------------
  2899. ; Add "A/C" load
  2900. ;--------------------------
  2901. L1094 brset port3Snap0, #$10, L1095 ; Branch if air conditioning switch is off (reverse logic)
  2902. addb #$04 ; A/C on, set flag b += 00000100
  2903.  
  2904. ;---------------------------------------------------------
  2905. ; Compute total rpm threshold from baseRpm+rpmOffset
  2906. ;---------------------------------------------------------
  2907. L1095 ldx #t_rpmEctOff ; x points to table of offsets
  2908. abx ; c points to desired offset
  2909. adda $00,x ; a = baseRpm+rpmOffset
  2910.  
  2911. ;---------------------------------------------------------------------
  2912. ; Based on that threshold, compute which state we will end-up with
  2913. ;---------------------------------------------------------------------
  2914. ;--------------------------------------------------------
  2915. ; If current rpm<threshold, use state1=00100000 (normal)
  2916. ;--------------------------------------------------------
  2917. cmpa rpm31 ; compare to current rpm
  2918. bhi L1099 ; Branch if current rpm lower than calculated value (engine is running normally?)
  2919. #ifdef E931
  2920. brset ftrimFlags, #$08, L1099 ; RPM is higher than threshold, branch anyway if speed>24km/h and IG2 related signal is set on E931??????
  2921. #endif
  2922.  
  2923. ;-----------------------------------------------------
  2924. ; rpm > threshold
  2925. ; if air volume low, use state1=00101100
  2926. ; i.e. runningFast and rotatingStopInj
  2927. ;-----------------------------------------------------
  2928. brset state2, #$08, L1096 ; Skip airVolT check / branch if no pulse accumulator interrupts received (mas broken, skip airFlow check?)
  2929. ldaa T2_airVolT ;
  2930. beq L1098 ; Branch if airVolT below threshold for more than 5s
  2931.  
  2932. ;-----------------------------------------------------
  2933. ; rpm > threshold and air volume high
  2934. ; If engine was started less than 5s ago, use state1=00100000 (normal)
  2935. ; i.e. high rev upon startup is normal
  2936. ;-----------------------------------------------------
  2937. L1096 ldaa T2_crank ;
  2938. adda #$0a ;
  2939. bcs L1099 ; branch if engine stopped "startingToCrank" less than 5s ago...
  2940. brset iscLrnFlags, #$20, L1099 ; branch if iscStStall has been updated
  2941.  
  2942. ;------------------------------------------------------------------------------
  2943. ; rpm > threshold and air volume high and engine started more than 10s ago
  2944. ; If idle switch is off, use state1=00100000 (normal)
  2945. ; i.e. we are stepping on the gas...
  2946. ;------------------------------------------------------------------------------
  2947. ldaa T40s_Idle ;
  2948. bne L1099 ; Branch if timer not yet 0, idle switch not on for more than 0.75s
  2949.  
  2950. ;---------------------------------------------------------------------------------
  2951. ; rpm > threshold and air volume high and engine started more than 10s ago
  2952. ; and idle switch has been on for more than 0.75s
  2953. ; use state1 = 00101100
  2954. ; i.e. runningFast and rotatingStopInj
  2955. ;---------------------------------------------------------------------------------
  2956. L1098 ldab #$2c ; b=00101100, this means the engine is running too fast
  2957. bra L1100 ;
  2958. L1099 ldab #$20 ; use b=00100000 (normal)
  2959.  
  2960. ;-------------------------------------------------------------
  2961. ; Set state1 flag if we are not receiving mas interrupts
  2962. ;-------------------------------------------------------------
  2963. L1100 brclr state2, #$08, L1101 ; Branch if pulse accumulator interrupts received
  2964. orab #$02 ; Set flag indicating we are not receiving pulse accumulator interrupts received
  2965.  
  2966. ;------------------------------------------------
  2967. ; At this point b has been set in preceeding
  2968. ; code to indicate current state, update state1
  2969. ;------------------------------------------------
  2970. L1101 ldaa state1 ;
  2971. anda #$80 ; Reset all except closed loop mode flag
  2972. aba ; Tranfser other flags set in code above
  2973. staa state1 ; Store new state
  2974.  
  2975. ;------------------------------------------------------
  2976. ; Compute index into maf compensation table t_masComp
  2977. ; since the values it contains are not equally spaced.
  2978. ; Basically remaps mafRaw16...
  2979. ;
  2980. ; Note that this mapping is the same for 1G and 2G
  2981. ; maf such that it doesn't need to be changed in case
  2982. ; 2G maf is used in 1G...
  2983. ;------------------------------------------------------
  2984. ldd mafRaw16 ; d = 16 bit mafRaw (a=mafRaw16/256)
  2985. ldy #L2054 ;
  2986. jsr pwiseLin ; d = T(L2054, mafRaw16) (a=T(L2054, mafRaw16/256))
  2987. std temp2 ; temp2:temp3 = T(L2054, mafRaw16)
  2988. jsr scale16m ; d = T(L2054, mafRaw16)/16 (b=16*T(L2054, mafRaw16/256))
  2989. cmpb #$80 ; Check for max of $80. Since max(T(L2054, mafRaw16/256))=20=1600Hz, we trim at $80/16=8=200Hz!
  2990. bcs L1103 ;
  2991. ldab #$80 ; Use max of $80
  2992.  
  2993. ;------------------------------------------------------------
  2994. ; Compute total maf compensation = masComp + t_masComp(Hz)
  2995. ;------------------------------------------------------------
  2996. L1103 stab temp5 ; temp5 = T(L2054, mafRaw16)/16 with max of $80
  2997. ldx #t_masComp ; x point to masCompensation table
  2998. ldd temp2 ; d = T(L2054, mafRaw16)
  2999. jsr interp1 ;
  3000. clra ; d = t_masComp(T(L2054, mafRaw16))
  3001. addb #masComp ; b = masComp + t_masComp(T(L2054, mafRaw16))
  3002. rola ; propagate carry bit in a,
  3003. std temp6 ; temp6:temp7 = d = t_masComp(T(L2054, mafRaw16)) = total MAS compensation
  3004.  
  3005. ;-------------------------------------------------------------------------
  3006. ; Compute conditioned L1992(iat) and compensate for barometric pressure
  3007. ;-------------------------------------------------------------------------
  3008. ldx #L1992 ;
  3009. jsr iatCInterp ; b = L1992(iat)
  3010. ldaa #$cd ; $cd is 1 bar for baroChecked
  3011. mul ; d = $cd*L1992(iat)
  3012. asld ; d = 2*$cd*L1992(iat)
  3013. div baroChecked ; d = 2*$cd*L1992(iat)/baroChecked
  3014. lsrb ; b = $cd*L1992(iat)/baroChecked = L1992(iat) * $cd/baroChecked = L1992(iat)*baroFactor where baroFactor=$cd/baroChecked equals 1.0 if baroChecked=1bar
  3015. adcb #$00 ; Round up result
  3016. ldx #$5222 ;
  3017. jsr clipOffset ; b = max(min(L1992(iat)*baroFactor,$52)-$22,0)
  3018.  
  3019. ;-----------------------------------------------------------------------
  3020. ; Compute airflow sensor linearity compensation factor from 2D table t_masLin
  3021. ; using max(min(L1992(iat)*baroFactor,$52)-$22,0)/16 for rows
  3022. ; and T(L2054, mafRaw16)/16 for columns, see t_masLin description
  3023. ;-----------------------------------------------------------------------
  3024. ldx #t_masLin ;
  3025. ldy #$0900 ;
  3026. ldaa temp5 ;
  3027. jsr lookup2D2 ; a = b = 2D interpolated t_masLin
  3028. stab masLinComp ;
  3029. jsr mul816_128 ; d = masLinComp * (masComp+t_masComp(xx))/128
  3030. std totMasComp ; totMasComp =(masComp+t_masComp(xx)) * t_masLin(xx)/128
  3031.  
  3032. ;-------------------------------------------------------------
  3033. ; Section to check if the o2 sensor is operating normally
  3034. ;-------------------------------------------------------------
  3035. ;-------------------------------------------------------------
  3036. ; If engine is notRotating, init rich/lean flag
  3037. ; and o2 sensor bad flag to default values
  3038. ;-------------------------------------------------------------
  3039. brclr state1, #$10, L1106 ; Branch if notRotating clear
  3040. orm closedLpFlags, #$c0 ; Assume o2Raw is rich and o2 sensor bad
  3041. ldaa o2Raw ; a = o2Raw
  3042. cmpa #$1f ;
  3043. bcc L1105 ; Branch if o2Raw >= 0.6v (rich)
  3044. andm closedLpFlags, #$7f ; o2Raw is lean, reset bit
  3045. L1105 bra L1114 ; Bail
  3046.  
  3047. ;-------------------------------------------------------
  3048. ; Choose how long to wait to check o2 sensor voltage
  3049. ; depending on ect (o2 sensor warm-up time...)
  3050. ;-------------------------------------------------------
  3051. L1106 ldaa T2_crank ; a = T2_crank
  3052. ldab ectFiltered ;
  3053. cmpb #$54 ; 41degC
  3054. bcs L1108 ; Branch if temperature(ectFiltered) > 41degC
  3055. adda #$58 ; a = T2_crank + $58 (44sec)
  3056. bra L1109 ;
  3057. L1108 adda #$1e ; a = T2_crank + $1e (15sec)
  3058.  
  3059. ;---------------------------------------------------------------------------------------
  3060. ; Update the rich/lean flag if sufficient time has elapsed since car was started
  3061. ;---------------------------------------------------------------------------------------
  3062. L1109 bcs L1114 ; bail if engine stopped "startingToCrank" less than 44 or 15 sec ago (depending en ect).
  3063. ldaa closedLpFlags ; a = old closedLpFlags
  3064. orm closedLpFlags, #$80 ; Assume result will be rich
  3065. ldab o2Raw ; b = o2Raw
  3066. cmpb #$1f ;
  3067. bcc L1111 ; Branch if o2Raw >= 0.6v (rich)
  3068. andm closedLpFlags, #$7f ; o2Raw is lean, Reset flag
  3069.  
  3070. ;--------------------------------------------------------
  3071. ; Check if flag value changed compared to the last time
  3072. ;--------------------------------------------------------
  3073. L1111 eora closedLpFlags ; Compare old closedLpFlags t new one
  3074. bmi L1112 ; Branch if rich/lean flag changed (reset o2 bad flag)
  3075.  
  3076. ;-----------------------------------
  3077. ; Rich/lean flag did not change
  3078. ;-----------------------------------
  3079. brclr state1, #$80, L1113 ; Reset timer and bail if open loop mode
  3080.  
  3081. ;----------------------------------------------
  3082. ; Closed loop mode and flag did not change yet
  3083. ; Check if timer is expired which would mean that
  3084. ; something is wrong (in closed loop mode, o2 sensor
  3085. ; voltage should have changed by now...)
  3086. ;----------------------------------------------
  3087. ldaa T2_o2Sensor ;
  3088. bne L1114 ; Bail if timer not yet expired
  3089. orm closedLpFlags, #$40 ; Timer expired, set o2 bad flag
  3090. bra L1114 ; Bail
  3091.  
  3092. ;------------------------------
  3093. ; Reset flag and restart timer
  3094. ;------------------------------
  3095. L1112 andm closedLpFlags, #$bf ; reset o2 bad flag
  3096. L1113 ldaa #$28 ; 20sec
  3097. staa T2_o2Sensor ; re-init timer to 20sec
  3098.  
  3099. ;--------------------------------------------------------------
  3100. ; Re-init T40_stInj0 to 1 sec if engine is not rotatingStopInj
  3101. ; This means that T40_stInj0 starts counting when state
  3102. ; changes to rotatingStopInj
  3103. ;--------------------------------------------------------------
  3104. L1114 brset state1, #$08, L1115 ; Branch if engine rotatingStopInj?
  3105. ldaa #$28 ; 1 sec
  3106. staa T40_stInj0 ;
  3107. bra L1116 ;
  3108.  
  3109. ;----------------------------------------------------------------
  3110. ; Re-init timer T40_stInj1 to 2 sec if engine
  3111. ; is rotatingStopInj and T40_stInj0 expired
  3112. ; This means that T40_stInj1 is only init when rotatingStopInj
  3113. ; has been active for more than 1 sec and will start counting
  3114. ; when rotatingStopInj is no more active. Will expire 2sec later.
  3115. ;----------------------------------------------------------------
  3116. L1115 ldaa T40_stInj0 ;
  3117. bne L1116 ; Branch if timer not expired
  3118. ldaa #$50 ;
  3119. staa T40_stInj1 ;
  3120.  
  3121. ;-------------------------------------------------------------
  3122. ; Section to decide between closed loop and open loop mode
  3123. ; (set/reset state1.7)
  3124. ;-------------------------------------------------------------
  3125. ;-------------------------------------------------------------
  3126. ; Have y point to airVol or airVolTB depending on baroChecked
  3127. ;-------------------------------------------------------------
  3128. L1116 ldy #airVol ;
  3129. ldaa baroChecked ;
  3130. cmpa #$9c ;
  3131. bcs L1117 ; Branch if baroChecked<0.76bar (I think??)
  3132. ldy #airVolTB ;
  3133.  
  3134. ;--------------------------------------------------------
  3135. ; Check airVolTB for first threshold (with hysteresis)
  3136. ;--------------------------------------------------------
  3137. L1117 ldx #t_closedLp1 ;
  3138. jsr interp16rpm ; b = t_closedLp1(rpm)
  3139. brclr closedLpFlags, #$01, L1118 ; Branch if we were not above the threshold the last time we checked
  3140. subb #$06 ; b = t_closedLp1(rpm)-6 (hysteresis)
  3141. bcc L1118 ; Branch if no underflow
  3142. clrb ; Use min of 0
  3143. L1118 andm closedLpFlags, #$fc ; Reset 000000011
  3144. cmpb $00,y ; Notice implicit y = y + 1 here!!!!!!!!!!!
  3145. bls L1119 ; Branch if t_closedLp1(rpm) <= airVol or airVolTB
  3146.  
  3147. ;---------------------------------------------------------
  3148. ; airVolTB smaller than threshold, closed loop is
  3149. ; therefore an option. Re-init timer T2_closedLp to 20sec
  3150. ; or 12sec and then continue trying to go to closed loop
  3151. ;---------------------------------------------------------
  3152. #ifdef E931
  3153. ldaa #$28 ; 20 sec
  3154. #else
  3155. ldaa #$18 ; 12 sec
  3156. #endif
  3157. staa T2_closedLp
  3158. bra L1122 ; Branch to continue closed loop checking
  3159.  
  3160. ;--------------------------------------------------------------------------------
  3161. ; At this point airVolTB is higher than first threshold. Normally,
  3162. ; the airflow is too high to be in closed loop mode but in order to account
  3163. ; for variations, we will remain in closed loop for a certain time
  3164. ; (T2_closedLp) as long as we are below a second threshold t_closedLp2(rpm). If
  3165. ; we go over that second threshold, we go to open loop immediatly. This is
  3166. ; implementing aiflow hysteresis under specific rpm conditions
  3167. ;
  3168. ; Check if airVolTB higher than second threshold (with an hysteresis of 6)
  3169. ;--------------------------------------------------------------------------------
  3170. L1119 orm closedLpFlags, #$01 ; Set flag indicating we are above the first threshold
  3171. #ifdef E931
  3172. ldx #t_closedLp2 ;
  3173. jsr interp16rpm ; b = t_closedLp2(rpm)
  3174. #else
  3175. jmp L1978 ; Jump to code patch for E932 rpm calculation..., jumps back here afterwards...
  3176. L1120 jsr interp16b ;
  3177. #endif
  3178. brset state1, #$80, L1121 ; Branch if closed loop mode
  3179. subb #$06 ; b = t_closedLp2(rpm)-6 (threshold hysteresis)
  3180. bcc L1121 ; Branch if underflow
  3181. clrb ; Use min of 0
  3182. L1121 decy ; y points back to airVol or airVolTB (implicit y=y+1 above...)
  3183. cmpb $00,y ; Notice implicit y = y+1 here!!!!!!!!!!!
  3184. bls L1126 ; Use open loop if airVolTB higher than second threshold
  3185.  
  3186. ;---------------------------------------------------------
  3187. ; airVolTB smaller than second threshold, we could
  3188. ; therefore remain in closed loop if timer not expired
  3189. ;
  3190. ; Check if T2_closedLp timer is expired
  3191. ;---------------------------------------------------------
  3192. ldaa T2_closedLp ;
  3193. beq L1126 ; Use open loop mode if T2_closedLp expired
  3194.  
  3195. ;----------------------------------------------------
  3196. ; Check tspRaw threshold with hysteresis to know
  3197. ; if closed loop is an option
  3198. ;
  3199. ; Go to open loop if
  3200. ; tpsRaw >= t_closedLp3(rpm)
  3201. ; Closed loop is possible if
  3202. ; tpsRaw < t_closedLp3(rpm)-$0d (hysteresis)
  3203. ;
  3204. ;----------------------------------------------------
  3205. L1122 ldx #t_closedLp3 ; x = t_closedLp3
  3206. jsr interp16rpm ; b = t_closedLp3(rpm)
  3207. brset state1, #$80, L1124 ; Branch if closed loop mode
  3208. subb #$0d ; b = t_closedLp3(rpm) - $0d
  3209. bcc L1124 ; Branch if no underflow
  3210. clrb ; Use min of 0
  3211. L1124 cmpb tpsRaw ;
  3212. bls L1126 ; Branch to use open loop if t_closedLp3(rpm)<= tpsRaw
  3213.  
  3214. ;-----------------------------------------------------
  3215. ; We could use closed loop, check a few more things
  3216. ;-----------------------------------------------------
  3217. brset state1, #$1b, L1126 ; Use open loop if engine is either notRotating or rotatingStopInj or runningFast
  3218. brset coilChkFlags, #$80, L1126 ; Use open loop if ignition problem is detected
  3219. brclr ftrimFlags, #$80, L1126 ; Use open loop if airVolT < threshold (15 or 24, used for fTrim...). Means airflow is too low???
  3220. orm closedLpFlags, #$02 ; Set flag indicating we should be using closed loop mode????
  3221. ldaa T40_stInj1 ;
  3222. bne L1126 ; Use open loop if rotatingStopInj has been active for more than 1 sec (and 2 sec after)
  3223. brset closedLpFlags, #$40, L1126 ; Use open loop if o2 sensor is bad
  3224. ldaa ectFiltered ;
  3225. #ifdef E931
  3226. cmpa #$6a ; 31degC
  3227. #else
  3228. cmpa #$70 ; 28degC
  3229. #endif
  3230.  
  3231. #ifdef noClosedLoop
  3232. bra L1126 ;
  3233. #else
  3234. bhi L1126 ; Use open loop if temperature(ectFiltered) < 31degC
  3235. #endif
  3236.  
  3237. ;-----------------------------------------------------
  3238. ; Use closed loop mode, set flag
  3239. ;-----------------------------------------------------
  3240. orm state1, #$80 ; Go into closed loop mode
  3241. bra L1127 ; Bail
  3242.  
  3243. ;---------------------------------
  3244. ; Use open loop mode, reset flag
  3245. ;---------------------------------
  3246. L1126 andm state1, #$7f ; Go into open loop mode
  3247.  
  3248. ;-----------------------------------
  3249. ; Reset o2Fbk in some cases
  3250. ; open loop for instance...
  3251. ;-----------------------------------
  3252. L1127 brset fpsBcsFlags, #$04, L1129 ; Branch to reset if the fuel pressure solenoid was just deactivated
  3253. brset varFlags0, #$02, L1130 ; Bail if hot start flag is set
  3254. brset state1, #$80, L1130 ; Bail if closed loop mode
  3255. L1129 ldd #$8080 ;
  3256. std o2Fbk ;
  3257.  
  3258. ;--------------------------------------------------
  3259. ; Re-init timer T40_o2Fbk to 4 sec if
  3260. ; currentTrimRange!=low (or high speed, E931 only)
  3261. ;
  3262. ; T40_o2Fbk will be 0 when the low trim range will
  3263. ; will have been used for more than 4 sec
  3264. ;--------------------------------------------------
  3265. L1130 brclr ftrimFlags, #$13, L1131 ; Branch if currentTrimRange=low and rpm<threshold
  3266. ldaa #$a0 ; 4sec
  3267. staa T40_o2Fbk ; T40_o2Fbk = 4sec
  3268.  
  3269. ;--------------------------------------------------------------------
  3270. ; Find which table we should be using depending on config resistors
  3271. ; (one of L1999, L2000, L2001, L2002)
  3272. ;--------------------------------------------------------------------
  3273. L1131 ldx #t_strap2 ;
  3274. jsr cfgLookup16 ; x = t_strap2(2*(config2 & $03)) = tableAddress
  3275.  
  3276. ;----------------------------------------------------------
  3277. ; Use o2Fbk_dec:o2Fbk_inc = t_o2Fbk1 if timer expired
  3278. ; i.e. we have been in low trim range for more than 4sec
  3279. ;----------------------------------------------------------
  3280. ldaa T40_o2Fbk ;
  3281. bne L1132 ; Branch if timer not expired
  3282. ldd t_o2Fbk1 ; Timer expired, use t_o2Fbk1
  3283. bra L1138 ; Bail to store
  3284.  
  3285. ;--------------------------------------------------------------------------
  3286. ; Timer not expired, compute values for o2Fbk_dec:o2Fbk_inc
  3287. ; from table pointed by x (see above, one of L1999, L2000, L2001, L2002)
  3288. ; Use b as index into table. Start with b = 0
  3289. ;--------------------------------------------------------------------------
  3290. L1132 clrb ; b = 0
  3291.  
  3292. ;-------------------------------------------------------------
  3293. ; b = b+1 if airVolTB >= threshold
  3294. ; Threshold based on config resistors (AWD vs FWD???)
  3295. ;-------------------------------------------------------------
  3296. ldaa airVolTB ; a = airVolTB
  3297. #ifdef E931
  3298. cmpa #$40 ;
  3299. brclr config2, #$80, L1133 ; Branch if ??? (same as branching if FWD)
  3300. cmpa #$40 ;
  3301. #else
  3302. cmpa #$48 ;
  3303. brclr config2, #$80, L1133 ;
  3304. cmpa #$50 ;
  3305. #endif
  3306. L1133 bcs L1134 ; Branch if airVolTB < threshold
  3307. incb ; airVolTB > threshold, b +=1 (go to next value)
  3308.  
  3309. ;-------------------------------------------------------------
  3310. ; b = b+2 if rpm31 >= (1500rpm or 1406rpm)
  3311. ; b+4 if rpm31 >= (2094rpm or 2313rpm)
  3312. ;-------------------------------------------------------------
  3313. L1134 ldaa rpm31 ;
  3314. cmpa #$2d ;
  3315. brclr config2, #$80, L1135 ; Branch if ??? (same as branching if FWD)
  3316. #ifdef E931
  3317. cmpa #$30 ;
  3318. #else
  3319. cmpa #$2d ;
  3320. #endif
  3321. L1135 bcs L1137 ; Branch if rpm31 < threshold (1500rpm or 1406rpm)
  3322. addb #$02 ; rpm31 > (1500rpm or 1406rpm, b += 2
  3323. cmpa #$43 ; 2094rpm
  3324. brclr config2, #$80, L1136 ; Branch if ??? (same as branching if FWD)
  3325. #ifdef E931
  3326. cmpa #$4a ; 2313rpm
  3327. #else
  3328. cmpa #$43 ; 2094rpm
  3329. #endif
  3330. L1136 bcs L1137 ;
  3331. addb #$02 ; rpm31 > (2313rpm or 2094rpm, b += 2
  3332.  
  3333. ;---------------------------------------------
  3334. ; interpolate table from x+b
  3335. ; (x is one of L1999, L2000, L2001, L2002)
  3336. ;---------------------------------------------
  3337. L1137 abx ;
  3338. ldaa $00,x ;
  3339. ldab $06,x ; ???????????
  3340.  
  3341. ;------------------------------------------------
  3342. ; Update o2Fbk_dec and o2Fbk_inc with new values
  3343. ;------------------------------------------------
  3344. L1138 std o2Fbk_dec
  3345.  
  3346. ;-------------------------------------------------------------
  3347. ; If we are in closed loop mode, limit the range of o2Fbk
  3348. ; depending on ect and then compute o2FuelAdj (how much
  3349. ; fuel to add/remove based on o2 sensor in closed loop....)
  3350. ;
  3351. ; Notice that part of the code has been located somewhere
  3352. ; else... (L1973)
  3353. ;-------------------------------------------------------------
  3354. ldaa #$80 ; pre-load default value of $80 (no fuel adjustment)
  3355. brclr state1, #$80, L1148 ; Branch if open loop mode
  3356.  
  3357. ;----------------------------------------------------
  3358. ; We are in closed loop mode, limit the range
  3359. ; of o2Fbk to $4d-$d6 or $2a-$d6 depending on ect
  3360. ;----------------------------------------------------
  3361. jmp L1973 ; Jump to code snipet for closed loop mode, will jump back to main code as appropriate
  3362. nop
  3363.  
  3364. ;------------------------------------------------
  3365. ; Continuation of code...
  3366. ;------------------------------------------------
  3367. ;---------------------------------------
  3368. ; temperature(ectFiltered) > 86degC
  3369. ; Check for min and max of $2a and $d6
  3370. ;---------------------------------------
  3371. L1140 bcc L1141 ; Branch if o2Fbk >= $2a
  3372. ldaa #$2a ; Use min of $2a
  3373. bra L1142 ;
  3374. L1141 cmpa #$d6 ;
  3375. bcs L1143 ; Branch if o2Fbk < $d6
  3376. ldaa #$d6 ; Use max of $d6
  3377.  
  3378. ;--------------------------
  3379. ; Store new value of o2Fbk
  3380. ;--------------------------
  3381. L1142 clrb ; Set lower 8 bit of o2Fbk
  3382. std o2Fbk ;
  3383.  
  3384. ;-----------------------------------------------------------------------------
  3385. ; Compute o2FuelAdj = o2Fbk +/- t_closedLpV1(xx) or t_closedLpV2(xx) or $02
  3386. ; where +/- depends on o2Raw (lean or rich).
  3387. ;-----------------------------------------------------------------------------
  3388. L1143 psha ; st0 = o2Fbk high byte
  3389. #ifdef E931 ;
  3390. ldab #$02 ; b = $02
  3391. ldaa T40_ftrim2 ; a = T40_ftrim2
  3392. beq L1146 ; Branch if timer expired, use $02 instead of table values if conditions are stable on E931
  3393. #else ;
  3394. ldx #t_closedLpV2 ; x = t_closedLpV2
  3395. brset port3Snap0, #$20, L1144 ; branch if Park/neutral
  3396. #endif
  3397. ldx #t_closedLpV1 ; x = t_closedLpV1
  3398. L1144 brclr ftrimFlags, #$13, L1145 ; branch if trim range is low and rpm<threshold???
  3399. inx ; go to next value in table
  3400. L1145 ldab $00,x ; b = = t_closedLpV1(xx) or t_closedLpV2(xx)
  3401. L1146 ldaa o2Raw ; a = o2Raw
  3402. cmpa #$1a ; 0.5V
  3403. pula ; a = o2Fbk
  3404. bcc L1147 ; Branch if o2Raw > 0.5V
  3405. aba ; o2 lean, a = o2Fbk + t_closedLpV1(xx) or t_closedLpV2(xx)
  3406. bcc L1148 ; branch if no overflow
  3407. ldaa #$ff ; Use max in case of overflow
  3408. bra L1148 ; Branch to store o2FuelAdj
  3409.  
  3410. L1147 sba ; o2 rich, a = o2Fbk - t_closedLpV1(xx) or t_closedLpV2(xx)
  3411. bcc L1148 ; branch if no underflow
  3412. clra ; Use min value of 0
  3413.  
  3414. ;---------------------
  3415. ; Store new o2FuelAdj
  3416. ;---------------------
  3417. L1148 staa o2FuelAdj ; o2Fbk +/- table value for fuel compensation in closed loop
  3418.  
  3419. ;-------------------------------------------------------------
  3420. ; Transfer ftrimFlags to oldFtrimFlg, a = old oldFtrimFlg
  3421. ;-------------------------------------------------------------
  3422. ldaa oldFtrimFlg ;
  3423. ldab ftrimFlags ;
  3424. stab oldFtrimFlg ;
  3425.  
  3426. ;--------------------------------------------------------------
  3427. ; Section to check whether conditions are sufficiently
  3428. ; stable to update fuel trims. Fuel trims are updated
  3429. ; only if T40_ftrim = 0
  3430. ;
  3431. ; Restart timer T40_ftrim at 5s under all the following conditions
  3432. ;
  3433. ; if fuel trim range changed
  3434. ; or open loop mode
  3435. ; or airVolTB too high
  3436. ; or ectRaw malfunction
  3437. ; or iatRaw malfunction
  3438. ; or baroRaw malfunction
  3439. ; or temperature(ectFiltered) < 86degC
  3440. ; or temperature(iatChecked) >= 50degC
  3441. ; or baroChecked < 0.76bar
  3442. ; or baroChecked >= 1.05bar
  3443. ; or accEnr not 0
  3444. ; or airDiffNeg1 >= accEnrDiffT
  3445. ; or airVolT too small
  3446. ; or purge solenoid activated
  3447. ; or fuel pressure solenoid activated
  3448. ; or T40_ftrim2 expired (E931)
  3449. ; or T0p5_crCold not expired
  3450. ;
  3451. ;--------------------------------------------------------------
  3452. eora ftrimFlags ;
  3453. anda #$03 ; a = (oldFtrimFlg eor ftrimFlags) & $03
  3454. bne L1150 ; Branch if trim rancge changed
  3455. brclr state1, #$80, L1150 ; Branch if open loop mode
  3456. brset closedLpFlags, #$01, L1150 ; Branch if airVolTB too high
  3457. brset state2, #$07, L1150 ; Branch if ectRaw, iatRaw or baroRaw in error
  3458. ldaa ectFiltered ;
  3459. #ifdef E931
  3460. cmpa #$1c ; 86degC
  3461. #else
  3462. cmpa #$1b ; 88degC
  3463. #endif
  3464. bhi L1150 ; Branch if temperature(ectFiltered) < 86degC
  3465. ldaa iatChecked ;
  3466. cmpa #$49 ;
  3467. bls L1150 ; Branch if temperature(iatChecked) >= 50degC
  3468. ldaa baroChecked ;
  3469. cmpa #$9c ;
  3470. bcs L1150 ; Branch if baroChecked < 0.76bar?
  3471. cmpa #$d8 ;
  3472. bcc L1150 ; Branch if baroChecked >= 1.05bar?
  3473. ldaa accEnr ;
  3474. bne L1150 ; Branch if accEnr not 0 (we are applying enrichment during acceleration)
  3475. ldaa airDiffNeg1 ;
  3476. cmpa accEnrDiffT ;
  3477. bcc L1150 ; Branch if airDiffNeg1 >= accEnrDiffT (we will apply decceleration enrichment????)
  3478. ldaa airVolT ;
  3479. cmpa #$18 ;
  3480. bcs L1150 ; Branch if airVolT < $18 (air volume too low)
  3481. brclr port6, #$10, L1150 ; Branch if purge solenoid activated
  3482. brclr port5, #$10, L1150 ; Branch if fuel pressure solenoid activated
  3483. #ifdef E931
  3484. ldaa T40_ftrim2 ;
  3485. beq L1150 ; Branch if timer T40_ftrim2 expired on E931???
  3486. #endif
  3487. ldaa T0p5_crCold ;
  3488. beq L1151 ; Branch if T0p5_crCold expired, meaning its been more than 120sec since we started a cold engine (we can update trims...)
  3489.  
  3490. ;-----------------------------------------------------
  3491. ; Conditions not stable, Re-init T40_ftrim at 5 sec
  3492. ;-----------------------------------------------------
  3493. L1150 ldaa #$c8 ; 5 sec
  3494. staa T40_ftrim ;
  3495.  
  3496. ;------------------------------------------------------------------
  3497. ; Get current fuel trim value according to current fuel trim range
  3498. ;------------------------------------------------------------------
  3499. L1151 ldx #ftrim_low ;
  3500. ldab ftrimFlags ;
  3501. andb #$03 ; Get current fuel trim range to update
  3502. abx ; X point to fuel trim
  3503. ldaa $00,x ; a = fuelTrim
  3504. ldab #$80 ; pre-load $80 in case we bail
  3505.  
  3506. ;----------------------------------------------
  3507. ; Don't update trim if T40_ftrim not yet expired
  3508. ;----------------------------------------------
  3509. tst T40_ftrim ;
  3510. bne L1153 ;
  3511.  
  3512. ;-------------------------------------------------------------------------------
  3513. ; Update fuel trim at 40Hz
  3514. ; fuel trim is actually increased/decreased by 1 at 40Hz/(256/5) = 0.78125Hz
  3515. ;-------------------------------------------------------------------------------
  3516. brclr Tclocks, #$01, L1154 ; Branch if 40Hz signal not set
  3517. ldab o2Fbk ; b = o2Fbk
  3518. cmpb #$80 ;
  3519. beq L1154 ; branch if o2Fbk = 100% (no update)
  3520. ldab ftrimCntr ; b = ftrimCntr
  3521. bcs L1152 ; branch if o2Fbk < 100%
  3522. addb #$05 ; b = ftrimCntr + 5
  3523. adca #$00 ; a = fuelTrim+1 if ftrimCntr rolled over (o2Fbk + 5)>255
  3524. bra L1153 ;
  3525. L1152 subb #$05 ; b = ftrimCntr - 5
  3526. sbca #$00 ; a = fuelTrim-1 if ftrimCntr rolled under (o2Fbk - 5)<0
  3527.  
  3528. ;------------------------------
  3529. ; Update ftrimCntr with new value
  3530. ;------------------------------
  3531. L1153 stab ftrimCntr ;
  3532.  
  3533. ;------------------------------------------------
  3534. ; Check a = updated trim value for min/max values
  3535. ;------------------------------------------------
  3536. L1154 cmpa #$68 ;
  3537. bcc L1155 ; Branch if new fuelTrim > $68 (81%)
  3538. ldaa #$68 ; use min
  3539. L1155 cmpa #ftrimMax ;
  3540. bls L1156 ; Branch if new fuelTrim <= max (~140%)
  3541. ldaa #ftrimMax ; use max
  3542.  
  3543. ;---------------------------------------------------------------
  3544. ; Update the stored fuel trim with updated value (in a) and decide
  3545. ; whether we will apply the fuel trim to injector pulse width
  3546. ;---------------------------------------------------------------
  3547. L1156 staa $00,x ; Store new fuel trim value
  3548. ldaa #$80 ; Assume we won't apply fuel trim, a = $80 (100% fuel trim)
  3549. brset state2, #$08, L1157 ; Branch to use 100% if pulse accumulator interrupts are not being received
  3550. brset closedLpFlags, #$01, L1157 ; Branch to use 100% if the "air volume (airVolTB) is too high to use closed loop mode (first threshold)"
  3551. ldaa $00,x ; Load fuel trim from the current range
  3552. L1157 staa workFtrim ; Store working fuel trim
  3553.  
  3554. ;--------------------------------------------------------
  3555. ; Compute coldTempEnr, fuel enrichment factor when engine
  3556. ; is cold...Depends on ect and airflow
  3557. ;
  3558. ; coldTempEnr/$80 = 1 + (f1-1)*f2
  3559. ; 1 + ectNetEnrichment*f2
  3560. ;
  3561. ; where f1 is t_ectEnr(ectCond)/$80, f1>=1.0
  3562. ; f2 is t_airEnr(airVolCond)/$80, f2>=1.0
  3563. ;
  3564. ; Basically this factor adds fuel enrichment under
  3565. ; cold temperature which is reduced down to no enrichement
  3566. ; (coldTempEnr/$80=1.0) as airVolCond increases, i.e. fuel
  3567. ; enrichement is only required under low temperature and
  3568. ; low airflow
  3569. ;--------------------------------------------------------
  3570. ldx #t_ectEnr ;
  3571. jsr interpEct ; b = t_ectEnr(ectCond)
  3572. clra ;
  3573. subb #$80 ; d = t_ectEnr(ectCond)-$80
  3574. bcc L1158 ; Branch if no overflow
  3575. clrb ; Use min of 0
  3576. L1158 std temp6 ; temp6:temp7 = t_ectEnr(ectCond)-$80
  3577. ldx #t_airEnr ;
  3578. ldaa airVolCond ;
  3579. jsr interp16b ; b = t_airEnr(airVolCond)
  3580. ldaa rpm31 ;
  3581. cmpa #$30 ;
  3582. bcc L1159 ;
  3583. ldab #$80 ; Use b=$80 if rpm<1500
  3584. L1159 jsr mul816_128 ; d = t_airEnr(airVolCond) * (t_ectEnr(ectCond)-$80)/$80
  3585. addb #$80 ; b = t_airEnr(airVolCond)*(t_ectEnr(ectCond)-$80)/$80 + $80
  3586. stab coldTempEnr ; coldTempEnr = t_airEnr(airVolCond) * (t_ectEnr(ectCond)-$80)/$80 + $80
  3587.  
  3588.  
  3589. ;----------------------------------------------------------------
  3590. ; Section to update openLoopEnr enrichment factor if in open loop
  3591. ;----------------------------------------------------------------
  3592. ldab #$80 ; Assume an enrichement factor of 1.0
  3593. brset state1, #$80, L1167 ; Branch if closed loop mode
  3594.  
  3595. ;-------------------------------------------------------------
  3596. ; Open loop
  3597. ; Compute conditionned rpm and load for 2D map interpolation
  3598. ;-------------------------------------------------------------
  3599. ldab rpm31 ; b = rpm31
  3600. ldaa #$d0 ; 6500 RPM
  3601. jsr rpmRange ; get rpm for map interpolation, b = min(max(RPM31p25-#$10, 0), $d0) = min(max(RPM31p25-500rpm,0),6500rpm)
  3602. stab temp6 ; temp6 = conditionned rpm
  3603. jsr getLoadForMaps ; get the load value for map interpolation
  3604. stab temp7 ; temp7 = conditionned load
  3605.  
  3606. ;----------------------------------------------
  3607. ; Get basic fuel enrichement from 2D fuel map
  3608. ;----------------------------------------------
  3609. ldx #t_fuelMap ;
  3610. ldy #$0e00 ;
  3611. jsr lookup2D ; a = b = 2D interpolated fuel map value from temp6 (rpm) and temp7 (load)
  3612.  
  3613. ;----------------------------------------
  3614. ; Check airVol vs. RPM (deceleration???)
  3615. ;----------------------------------------
  3616. ldaa airVol ;
  3617. cmpa #$af ;
  3618. bcs L1162 ; branch if airVol<$af
  3619. ldaa rpm31 ;
  3620. #ifdef E931
  3621. cmpa #$46 ; 2187rpm
  3622. #else
  3623. cmpa #$53 ; 2594rpm
  3624. #endif
  3625. bcs L1162 ; branch if current rpm smaller than threshold
  3626. ldaa timFuelEnr ; a = timing/knock based fuel enrichement
  3627. aba ; a = basicFuelEnrichement + timingKnockFuelEnrichment
  3628. bcs L1163 ; branch if overflow
  3629. tab ; b = basicFuelEnrichement + timingKnockFuelEnrichment
  3630.  
  3631. ;-----------------------------------------------------------------------
  3632. ; Check fuel compensation + timing/knock based enrichment for max value
  3633. ;-----------------------------------------------------------------------
  3634. L1162 cmpb #fuelMapClip ;
  3635. bls L1164 ; Branch if below max
  3636. L1163 ldab #fuelMapClip ; Use max
  3637. L1164 pshb ; Store basicFuelEnrichement + timingKnockFuelEnrichment on stack
  3638.  
  3639. ;-----------------------------------
  3640. ; Compute TPS based fuel enrichment
  3641. ;-----------------------------------
  3642. ldab tpsRaw ;
  3643. ldx #$b080 ;
  3644. jsr clipOffset ; b = max(min(tpsRaw,$b0)-$80,0)-> returns b = $00 to $30 (50% to 69%)
  3645. tba ; a = conditionned TPS for table interpolation
  3646. ldx #t_tpsEnr ;
  3647. jsr interp16b ; a = t_tpsEnr(tpsRaw)
  3648.  
  3649. ;-----------------------------------------------------------------------------------------
  3650. ; Keep the highest of t_tpsEnr(tpsRaw) and "basicFuelEnrichement + timingKnockFuelEnrichment"
  3651. ;-----------------------------------------------------------------------------------------
  3652. pulb ; b = basicFuelEnrichement + timingKnockFuelEnrichment
  3653. cba ;
  3654. bcs L1165 ; branch if basicFuelEnrichement + timingKnockFuelEnrichment
  3655. tab ; b = max(t_tpsEnr(tpsRaw), basicFuelEnrichement + timingKnockFuelEnrichment)
  3656.  
  3657.  
  3658. ;-----------------------------------------------------------
  3659. ; Compute timer T2_hotEnrich based fuel enrichement if required
  3660. ;-----------------------------------------------------------
  3661. L1165 brclr varFlags0, #$02, L1167 ; Bail if "hot start" flag was not set
  3662. pshb ; save on stack
  3663. ldab #$30 ;
  3664. ldaa T2_hotEnrich ;
  3665. mul ;
  3666. adda #$80 ; d = T2_hotEnrich*$30 + $80
  3667.  
  3668. ;---------------------
  3669. ; Keep the highest
  3670. ;---------------------
  3671. pulb ; b = max(t_tpsEnr(tpsRaw), basicFuelEnrichement + timingKnockFuelEnrichment)
  3672. cba ;
  3673. bls L1167 ;
  3674. tab ; max(T2_hotEnrich*$30 + $80, t_tpsEnr(tpsRaw), basicFuelEnrichement + timingKnockFuelEnrichment)
  3675.  
  3676. ;-------------------------------------------
  3677. ; Update openLoopEnr with the above result
  3678. ;-------------------------------------------
  3679. L1167 stab openLoopEnr ; Store final value
  3680.  
  3681. ;------------------------------------------------------------------
  3682. ; Update T2_hotEnrich and varFlags0.1 (hot start) used in timer based enrichement above
  3683. ;------------------------------------------------------------------
  3684. brclr state1, #$90, L1168 ; Branch if notRotating and closed loop clear
  3685. andm varFlags0, #$fd ; closedLoop or notRotating, reset flag and reset timer
  3686. clra ; a = 0
  3687. bra L1169 ; Update timer with 0
  3688.  
  3689. L1168 brclr state1, #$01, L1171 ; open loop and at least rotating, Bail if startingToCrank clear
  3690. ldaa iatChecked ; Engine startingToCrank, a = iatChecked
  3691. cmpa #$3a ;
  3692. bhi L1171 ; Bail if temperature(iatChecked) < 60degC
  3693. ldaa ectFiltered ;
  3694. cmpa #$18 ; 93degC
  3695. bhi L1171 ; Bail if temperature(ectFiltered) < 93degC
  3696.  
  3697. ;-----------------------------------------------------------------------------
  3698. ; At this point, engine is startingToCrank and we are in open loop
  3699. ; and temperature(iatChecked) >= 60degC and
  3700. ; temperature(ectFiltered) >= 93degC (hot start)
  3701. ;
  3702. ; Set hot start flag, init o2Fbk to lean(??) and re-init T2_hotEnrich timer to 120sec
  3703. ;-----------------------------------------------------------------------------
  3704. orm varFlags0, #$02 ; Set varFlags0.1 flag (hot start)
  3705. ldaa #$d6 ;
  3706. ldab #$80 ;
  3707. std o2Fbk ; o2Fbk = $d680 (lean?)
  3708. ldaa #$f0 ; Re-init timer with 120sec
  3709. L1169 staa T2_hotEnrich ;
  3710.  
  3711. ;----------------------------------------------
  3712. ; Update enrWarmup when engine startingToCrank
  3713. ; get enrWarmup timer value from table
  3714. ;----------------------------------------------
  3715. L1171 clrb ;
  3716. brset state1, #$10, L1172 ; Branch if notRotating
  3717. brclr state1, #$01, L1173 ; Branch if startingToCrank clear
  3718. ldx #t_enrWarmup ;
  3719. jsr interpEct ;
  3720. L1172 stab enrWarmup ;
  3721. bra L1174 ;
  3722.  
  3723. ;-----------------------------------------------------------------------------------------
  3724. ; Update enrWarmup at 40Hz
  3725. ;
  3726. ; T_enrWarm is decremented at 40Hz and loops at 2 if enrWarmup>$1a or loops
  3727. ; at $18 otherwise decrement enrWarmup each time T_enrWarm reaches 0
  3728. ;
  3729. ; This allows to have rapid lowering of enrWarmup at first and then slow one
  3730. ; enrWarmup is decremented at 20Hz until it reached $1a (fuel enrichment factor of 141%)
  3731. ; and then at 1.67Hz
  3732. ;-----------------------------------------------------------------------------------------
  3733. L1173 brclr Tclocks, #$01, L1176 ; Bail if 40Hz signal not set
  3734. ldaa enrWarmup ;
  3735. beq L1176 ;
  3736. dec T_enrWarm ;
  3737. bne L1176 ;
  3738. dec enrWarmup ;
  3739.  
  3740. L1174 ldaa #$02 ;
  3741. ldab enrWarmup ;
  3742. cmpb #$1a ;
  3743. bhi L1175 ; Branch if enrWarmup>$1a
  3744. ldaa #$18 ; Reset T_enrWarm counter to $18
  3745. L1175 staa T_enrWarm ;
  3746.  
  3747.  
  3748. ;------------------------------------------------
  3749. ; Section to compute injFactor
  3750. ; start with basic value of totMasComp*16
  3751. ;------------------------------------------------
  3752. L1176 ldd totMasComp ; d = totMasComp
  3753. asld ;
  3754. asld ;
  3755. asld ;
  3756. asld ;
  3757. std temp6 ; temp6:temp7 = totMasComp*16
  3758.  
  3759. ;---------------------------------------
  3760. ; Factor in injector size compensation
  3761. ;---------------------------------------
  3762. ldab #injComp ; Injector size compensation factor ($80 = 100% = no compensation, referenced at 260cc, 36psi)
  3763. jsr mul816_128 ; d = [temp6:temp7] = totMasComp*16 * injComp /128
  3764. std injMasComp ; injMasComp = totMasComp*16 * injComp/128
  3765.  
  3766. ;-------------------------------------------------------
  3767. ; Factor in working fuel trim and 02 fuel adjustment
  3768. ; Done this way, total range is limited to [50%,150%]
  3769. ; when both workFtrim and o2FuelAdj are at $00 or $ff
  3770. ;-------------------------------------------------------
  3771. clra ; a=0
  3772. ldab workFtrim ; d = working fuel trim ($80=100%)
  3773. addd #$0100 ; d = workFtrim + 2*$80
  3774. addb o2FuelAdj ; Add o2 adjustment (Add/remove fuel based on o2 sensor voltage/feedback, $80=100%->no fuel adjustment)
  3775. adca #$00 ; propagate carry, d = workFtrim + o2FuelAdj + 2*$80
  3776. jsr mul1616_512 ; D = [temp6:temp7] = [workFtrim + o2FuelAdj + 2*$80]/512 * [temp6:temp7]
  3777.  
  3778. ;-----------------------------------
  3779. ; Factor-in air temp, baro, etc...
  3780. ;-----------------------------------
  3781. ldab iatCompFact ; Correct for air temperature (air density)
  3782. jsr mul816_128 ; D and [temp6:temp7] = b*[temp6:temp7]/128
  3783. ldab baroFact ; Correct for barometric pressure (air density)
  3784. jsr mul816_128 ; D and [temp6:temp7] = b*[temp6:temp7]/128
  3785. ldab openLoopEnr ; Apply the open loop enrichment factor, based on timing/knock, tps and timer
  3786. jsr mul816_128 ; D and [temp6:temp7] = b*[temp6:temp7]/128
  3787. ldab coldTempEnr ; Add fuel enrichement under cold engine temperature and low airflow
  3788. jsr mul816_128 ; D and [temp6:temp7] = b*[temp6:temp7]/128
  3789. ldab enrWarmup ; b = enrichment during warmup/startup, from t_enrWarmup(ECT), can reach 300% in very cold temp but is decreased to 140% very rapidly
  3790. clra ; a=0
  3791. asld ; d = 2*enrWarmup
  3792. addd #$0080 ; d = 2*enrWarmup + $80
  3793. jsr mul1616_128 ; Apply (2*enrWarmup+$80) enrichment factor
  3794. std injFactor ; Store final result in injFactor (global injector factor)
  3795.  
  3796. ;----------------------------
  3797. ; Compute injector deadTime
  3798. ;----------------------------
  3799. ldx #t_deadtime-2
  3800. ldaa battRaw
  3801. jsr interp32
  3802. stab deadTime
  3803.  
  3804. ;-----------------------------------------
  3805. ; Update injFlags0.2 (set but not reset???)
  3806. ;-----------------------------------------
  3807. ldaa rpm31
  3808. cmpa #$0e
  3809. bcs L1178 ; branch if rpm < 437.5
  3810. orm injFlags0, #$04 ; rpm >= 437.5, set bit
  3811.  
  3812. ;-----------------------------------------------
  3813. ; Update injFlags0.1.3.5 if engine notRotating
  3814. ;-----------------------------------------------
  3815. L1178 brclr state1, #$10, L1180 ; Branch if notRotating clear
  3816. andm injFlags0, #$df ; assume we reset 00100000, updated below
  3817. ldaa ectFiltered ;
  3818. cmpa #$c2 ; -8degC
  3819. bcs L1179 ; Branch if temperature(ectFiltered) > -8degC
  3820. orm injFlags0, #$20 ; temperature(ectFiltered) <= -8degC, set bit
  3821. L1179 brclr injFlags0, #$04, L1180 ; Branch if rpm<437.5
  3822. andm injFlags0, #$fa ; Reset 0000 0101
  3823.  
  3824. ;------------------------------------------------------------------------------------
  3825. ; Section to compute injPwStart, the injector pulsewidth when engine "startingToCrank"
  3826. ;
  3827. ; startingToCrank "cold engine" injPwStart injFlags0.7 (startingToCrankColdEngine)
  3828. ; 0 0 0 0
  3829. ; 0 1 0 0
  3830. ; 1 0 pulseWidth*4 0
  3831. ; 1 1 pulseWidth 1
  3832. ;------------------------------------------------------------------------------------
  3833. ;--------------------------------
  3834. ; Get starting value from L2008
  3835. ;--------------------------------
  3836. L1180 clra
  3837. brclr state1, #$01, L1191 ; Bail to end of section if startingToCrank clear
  3838. ldx #L2008 ; Engine is startingToCrank
  3839. jsr interpEct2 ; b = L2008(ectCond)
  3840. ldaa #$80 ; a = $80
  3841. mul ;
  3842. lsrd ; d = $80*L2008(ectCond)
  3843. std temp6 ; [temp6:temp7] = $80*L2008(ectCond)
  3844.  
  3845. ;--------------------------------------------
  3846. ; Factor-in some enrichement if injCount<5
  3847. ; This only adds more fuel when starting to
  3848. ; crank for the first time under very cold
  3849. ; temperature (-16degC). Not sure as to
  3850. ; exactly why but at -16degC, I guess
  3851. ; it just might help???
  3852. ;--------------------------------------------
  3853. ldaa injCount ;
  3854. cmpa #$05 ;
  3855. bcc L1182 ; Branch if injCount>=5
  3856. ldx #L2042 ;
  3857. jsr interpEct2 ; b=L2042(ectCond)
  3858. clra ;
  3859. addd #$0080 ; d = L2042(ectCond) + $80
  3860. jsr mul1616_128 ; [temp6:temp7] = [temp6:temp7] * (L2042(ectCond)+$80)/128
  3861.  
  3862. ;------------------------------------------------------------
  3863. ; Factor in an rpm dependent correction factor if rpm >125???
  3864. ;------------------------------------------------------------
  3865. L1182 ldaa rpm8 ; a = rpm8
  3866. cmpa #$40 ;
  3867. bcs L1183 ; Branch if rpm < max of 500
  3868. ldaa #$40 ; rpm>500, use max of 500rpm
  3869.  
  3870. L1183 suba #$10 ; a = rpm8-$10
  3871. bcs L1185 ; Bail if rpm8<125rpm
  3872. asla ; a = 2*(rpm8-$10)
  3873. #ifdef E931
  3874. ldab #$56 ;
  3875. #else
  3876. ldab #$57 ;
  3877. #endif
  3878. mul ; d = $56*2*(rpm8-$10)
  3879. asld ;
  3880. tab ; b = 2*$56*2*(rpm8-$10)/256
  3881. ldaa #$80 ; a = $80
  3882. sba ; a = $80 - $56*2*(rpm8-$10)/128
  3883. bcc L1184 ; Branch if no underflow
  3884. clra ; Use min of 0
  3885. L1184 tab ; b = $80 - $56*2*(rpm8-$10)/128
  3886. jsr mul816_128 ; [temp6:temp7] = [temp6:temp7] * ($80 - $56*2*(rpm8-$10)/128)/128
  3887.  
  3888.  
  3889. ;------------------------------------------------------------------
  3890. ; Check current value for minimum, keep min
  3891. ; minimum is L2008(0) -> starting value at 86degC (hot engine)
  3892. ;------------------------------------------------------------------
  3893. L1185 ldab L2008 ; b = L2008(0)
  3894. ldaa #$80 ; a = $80
  3895. mul ; d = $80*L2008(0)
  3896. lsrd ; d = $80*L2008(0)/2
  3897. cmpd1 temp6 ;
  3898. bcs L1187 ; branch if current value >
  3899. std temp6 ;
  3900.  
  3901. ;--------------------------------
  3902. ; Factor-in barometric pressure
  3903. ;--------------------------------
  3904. L1187 ldab baroFact ;
  3905. jsr mul816_128 ; d = pulseWidth = [temp6:temp7] = [temp6:temp7] * baroFact/128
  3906.  
  3907. ;-----------------------------------------
  3908. ; Multiply by 2 and check for overflow
  3909. ;-----------------------------------------
  3910. asld ; pulseWidth = d = 2*[temp6:temp7]
  3911. bcc L1188 ; Branch if no overflow
  3912. ldaa #$ff ; Use max
  3913.  
  3914. ;--------------------------------------------------------------
  3915. ; At this point d = pulseWidth
  3916. ;
  3917. ; Set startingToCrankColdEngine flag if pulseWidth > threshold
  3918. ; or reset it (with hysteresis)
  3919. ;
  3920. ; Note that pulseWidth will be larger when the engine is cold, also rpm dependent...
  3921. ;--------------------------------------------------------------
  3922. L1188 pshb ; Put b on stack for temp calculation
  3923. ldab #$35 ; b = $35 = 13.6ms =threshold
  3924. brclr injFlags0, #$80, L1190 ; Branch if startingToCrankColdEngine was not previously set
  3925. ldab #$30 ; Flag was set, use lower threshold (hysteresis) b=$30 = 12.3ms
  3926. L1190 orm injFlags0, #$80 ; By default, assume startingToCrankColdEngine flag set
  3927. cba ;
  3928. pulb ; restore b (lower part of pulseWidth)
  3929. bcc L1192 ; Branch if pulseWidth/256 >= threshold (flag already set), we are startingToCrankColdEngine
  3930.  
  3931. ;-------------------------------------------------------------------------
  3932. ; pulseWidth < threshold, engine is therefore startingToCrank
  3933. ; but the engine is not cold
  3934. ; Multiply pulseWidth by 4?????? and reset the flag
  3935. ;-------------------------------------------------------------------------
  3936. asld ;
  3937. asld ; d = d * 4
  3938. L1191 andm injFlags0, #$7f ; Reset startingToCrankColdEngine flag
  3939. L1192 xgdx ; x = pulseWidth
  3940.  
  3941. ;-----------------------------------------------------
  3942. ; Store injPwStart and update state3.7 from injFlags0.7
  3943. ; (done this way since used in interrupts)
  3944. ;-----------------------------------------------------
  3945. ldaa injFlags0 ;
  3946. anda #$80 ; a = injFlags0 & $80, keep only that bit
  3947. sei ;
  3948. stx injPwStart ; injPwStart = pulseWidth
  3949. ldab state3
  3950. andb #$7f ;
  3951. aba ; b = b&$7f + injFlags0&$80
  3952. staa state3 ; Update state3
  3953.  
  3954. ;----------------------------------------
  3955. ; Update state3 from state1
  3956. ;----------------------------------------
  3957. ldaa state1 ;
  3958. anda #$1b ; Keep 00011011
  3959. brclr state1, #$08, L1193 ; Branch if rotatingStopInj clear
  3960. brset state1, #$04, L1193 ; Engine rotatingStopInj, branch if runningFast
  3961. oraa #$20 ; Engine rotatingStopInj but not runningFast, set bit
  3962. L1193 ldab state3 ;
  3963. andb #$c4 ; Keep 11000100
  3964. aba ;
  3965. staa state3 ;
  3966.  
  3967. ;--------------------------------------------------------------------------
  3968. ; If engine is just startingToCrank and this is the first time we are here
  3969. ; then compute sInjPw and schedule an interrupt to activate simultaneous
  3970. ; injection (if no injector is currently active)
  3971. ;--------------------------------------------------------------------------
  3972. cli ;
  3973. brclr state1, #$01, L1195 ; Bail if startingToCrank clear
  3974. brset injFlags0, #$01, L1195 ; Engine startingToCrank, bail if injFlags0.0 already set, meaning we were already here before and
  3975. orm injFlags0, #$01 ; Set flag
  3976. ldaa #$0c ; a = $0c
  3977. clrb ; d = $0c00
  3978. sei ;
  3979. std sInjPw ; sInjPw = $0c00 = 3.072ms
  3980. brset injToAct, #$0f, L1194 ; Bail if any injector is active
  3981. ldd t1t2_clk ; No injector flag set, schedule interrupt
  3982. addd #$0014 ; 20us
  3983. std t1_outCmpWr ; schedule interrupt in 20us
  3984.  
  3985. ;----------------------
  3986. ; Compute accEnrFact
  3987. ;----------------------
  3988. L1194 cli ;
  3989. L1195 ldx #L2051 ; x point to L2051
  3990. jsr interpEct ; b = L2051(ect)
  3991. ldx #t_accEnr1 ; x point to t_accEnr1
  3992. ldy #t_accEnr2a ;
  3993. addb T2_crank ;
  3994. bcc L1196 ; branch if engine stopped "startingToCrank" more than L2051(ect)/2 sec ago.
  3995. ldy #t_accEnr2b ;
  3996. L1196 jsr L1577 ;
  3997. std accEnrFact ; [accEnrFact:accEnrFact+1] = 8 * injMasComp * t_accEnr1(rpm)/128 * [t_accEnr2a(ect) or t_accEnr2b(ect)]/128 * baroFact/128
  3998.  
  3999. ;-----------------------
  4000. ; Compute accEnrDecay
  4001. ;-----------------------
  4002. ldx #t_accEnrDecay ;
  4003. jsr interpEct ;
  4004. stab accEnrDecay ; accEnrDecay = t_accEnrDecay(ect)
  4005.  
  4006. ;---------------------
  4007. ; Compute accEnrMinAf
  4008. ;---------------------
  4009. ldx #L2039
  4010. jsr interp16rpm ; a = L2039(rpm)
  4011. ldab #$57 ;
  4012. mul ; d = $57*L2039(rpm)
  4013. jsr scale16 ; d = $57*L2039(rpm)/16
  4014. std accEnrMinAf ; accEnrMinAf:L0106 = $57*L2039(rpm)/16
  4015.  
  4016. ;---------------------
  4017. ; Compute decEnrFact
  4018. ;--------------------- ;
  4019. ldaa T40_crank ;
  4020. adda #$78 ; 3s
  4021. bcc L1197 ; branch if engine stopped "startingToCrank" more than 3s ago
  4022. clra ;
  4023. clrb ;
  4024. bra L1199 ;
  4025. L1197 ldx #t_decEnr1 ;
  4026. ldy #t_decEnr2 ;
  4027. jsr L1577 ;
  4028. L1199 std decEnrFact ; [decEnrFact:decEnrFact+1] = 8 * injMasComp * t_decEnr1(rpm)/128 * t_decEnr2/128 * baroFact/128
  4029.  
  4030.  
  4031. ;-------------------------------------------------------------------------
  4032. ; Compute sInjEnrInc
  4033. ; Parameters related to adding fuel during simultaneous injection mode
  4034. ;-------------------------------------------------------------------------
  4035. ldx #L2051 ;
  4036. jsr interpEct ; b = L2051(ect)
  4037. ldx #L2013 ;
  4038. addb T2_crank ; b = L2051(ect) + T2_crank
  4039. bcc L1202 ; branch if engine stopped "startingToCrank" more than L2051(ect)/2 sec ago
  4040. ldx #L2050 ; Overflow, change table
  4041. L1202 jsr interpEct ; b = L2050(ect) or L2013(ect)
  4042. pshb ; st0 = L2050(ect) or L2013(ect)
  4043. ldx #L2015 ;
  4044. jsr interp16rpm ; b = L2015(rpm)
  4045. pula ; a = L2050(ect) or L2013(ect)
  4046. aba ; a = L2050(ect) or L2013(ect) + L2015(rpm)
  4047. bcc L1205 ; Branch if no overflow
  4048. ldaa #$ff ; Overflow, use max
  4049. L1205 staa sInjEnrInc ; sInjEnrInc = L2050(ect) or L2013(ect) + L2015(rpm)
  4050.  
  4051. ;-------------------------------------------------------------------------
  4052. ; Compute sInjEnrMax = sInjEnrInc/4
  4053. ; Parameters related to adding fuel during simultaneous injection mode
  4054. ;-------------------------------------------------------------------------
  4055. ldab #$20 ; b = $20
  4056. mul ; d = $20 * sInjEnrInc
  4057. jsr scale128m ; d = $20/128 * sInjEnrInc = 1/4 * sInjEnrInc, also check for max of 255...
  4058. stab sInjEnrMax ; sInjEnrMax = 1/4 * sInjEnrInc = 1/4 * (L2051(ect) + T2_crank or L2050(ect) + L2015(rpm))
  4059.  
  4060. ;---------------------------------------------------------------------------------
  4061. ; Compute sInjTpsMax, threshold used to increase fuel in simulateneous injection
  4062. ;---------------------------------------------------------------------------------
  4063. ldx #t_sInjTpsMax ;
  4064. jsr interp16rpm ;
  4065. stab sInjTpsMax ; sInjTpsMax = t_sInjTpsMax(rpm)
  4066.  
  4067. ;---------------------------------
  4068. ; Decrement T40s_casInt at 40Hz
  4069. ;---------------------------------
  4070. ldx #T40s_casInt
  4071. jsr decX40Hz ; Decrement T40s_casInt at 40Hz
  4072.  
  4073. ;-------------------------------------------------------------------------
  4074. ; Set the timing adjustment flag (timAdjFlags.7) if the timing
  4075. ; adjustment terminal is grounded but the ECU test mode terminal is not
  4076. ;-------------------------------------------------------------------------
  4077. brclr port4Snap, #$10, L1209 ; Branch if stored timing terminal not grounded?
  4078. brclr port4, #$10, L1209 ; Branch if timing terminal not grounded?
  4079. brset port4Snap, #$08, L1209 ; Branch if ECU test mode terminal grounded?
  4080. orm timAdjFlags, #$80 ; Set flag, timing terminal grounded but ECU test mode terminal NOT grounded
  4081. bra L1210 ; Bail
  4082. L1209 andm timAdjFlags, #$7f ; Reset flag
  4083.  
  4084. ;-----------------------------------------------------
  4085. ; Re-init knockTimer to $ff if engine notRotating
  4086. ;-----------------------------------------------------
  4087. L1210 brclr state1, #$10, L1212 ; Branch if notRotating clear
  4088. ldaa #$ff ; Engine notRotating
  4089. staa knockTimer
  4090.  
  4091. ;-----------------------------------------------------
  4092. ; Update the "knock sensor bad" flag if
  4093. ; engine was started more than 1 sec ago
  4094. ;-----------------------------------------------------
  4095. L1212 brset state1, #$11, L1214 ; Branch if notRotating or startingToCrank
  4096. ldaa T40_crank ; Engine is running
  4097. adda #$28 ;
  4098. bcs L1214 ; branch if engine stopped "startingToCrank" less than 1s ago (don't check knock sensor yet)
  4099. orm knockFlags, #$40 ; Set flag indicating "engine running for more than 1 sec"???
  4100. brset port4Snap, #$20, L1215 ; Branch if knock sensor is OK???
  4101. orm state2, #$20 ; Set flag indicating knock sensor is bad
  4102. bra L1217 ; Bail
  4103. L1214 andm knockFlags, #$bf ; Reset flag indicating "engine running for more than 1 sec"???
  4104. L1215 andm state2, #$df ; Reset bad knock sensor flag
  4105.  
  4106. ;---------------------------------------------------------------------------------
  4107. ; Update knockFlags.7 (airVol threshold flag) and T200s_knock if below threshold
  4108. ;---------------------------------------------------------------------------------
  4109. L1217 ldab airVol ; b=airVol
  4110. cmpb #$49 ;
  4111. bcc L1218 ; branch if airVol>$49
  4112. andm T200s_knock, #$03 ; airVol<$49, reset T200s_knock to a more reasonable value
  4113. andm knockFlags, #$7f ; clear knockFlags.7
  4114. bra L1219 ; branch LDB89
  4115. L1218 orm knockFlags, #$80 ; set knockFlags.7
  4116.  
  4117. ;-----------------------------------------------------
  4118. ; Something activated at 4595rpm with hysteresis??????
  4119. ; Could be related to knock since we are in the area...
  4120. ; Maybe knock sensor filter parameter being changed???
  4121. ;-----------------------------------------------------
  4122. L1219 ldaa #$96 ; 4688rpm
  4123. brclr port6, #$02, L1220 ; branch if ??? not yet activated???
  4124. ldaa #$90 ; 4500rpm
  4125. L1220 cmpa rpm31 ;
  4126. bhi L1221 ; Branch if rpm lower than threshold
  4127. orm port6, #$02 ; rpm higher than threshold, Activate ???
  4128. bra L1222 ;
  4129. L1221 andm port6, #$fd ; De-activate ???
  4130.  
  4131. ;----------------------------
  4132. ; Section to update octane
  4133. ;----------------------------
  4134. ;--------------------------------------------------
  4135. ; Skip octane update if temp(ectFiltered) < 80degC
  4136. ;--------------------------------------------------
  4137. L1222 ldaa ectFiltered
  4138. cmpa #$20 ; 80degC
  4139. bhi L1227 ; Bail if temp(ectFiltered) < 80degC
  4140.  
  4141. ;-----------------------------------------
  4142. ; temp(ectFiltered) >= 80degC
  4143. ; Skip octane update under more cases...
  4144. ;-----------------------------------------
  4145. brset state2, #$28, L1227 ; Bail if no pulse accumulator interrupts or if knock sensor not working?
  4146. brclr knockFlags, #$40, L1227 ; Bail if engine has not been running for more than 1 sec
  4147. ldy #L2052 ; Engine has been running for more than 1 sec
  4148. jsr rpmPwise ; b = piecewise(rpm4) for table interpolation
  4149. ldaa #$b0 ; a = $b0
  4150. jsr abmin ; Apply max to b, b = min($b0,piecewise(rpm4))
  4151. tba ; a = min($b0,piecewise(rpm4))
  4152. ldx #L2041 ; x points to L2041
  4153. jsr interp16b ; a = L2041(rpm4)
  4154. cmpa airVol ;
  4155. bcc L1227 ; Bail if airVol <= L2041(rpm4)
  4156.  
  4157. ;---------------------------------------------
  4158. ; airVol > L2041(rpm4), skip octane update
  4159. ; if 3<=knockSum<=5 (hysteresis zone...)
  4160. ;---------------------------------------------
  4161. ldaa knockSum ;
  4162. cmpa #$03 ;
  4163. bcs L1223 ; Branch if knockSum <3
  4164. cmpa #$05 ;
  4165. bls L1227 ; Bail if knockSum <=5
  4166.  
  4167. ;-------------------------------------------------------------
  4168. ; knockSum<3 or knockSum>5, we can update octane at 2.5Hz
  4169. ;-------------------------------------------------------------
  4170. L1223 rora ; shift in carry bit, rest of a = knockSum/2
  4171.  
  4172. ;-----------------------------------------------------------
  4173. ; Decrement T40s_octane at 40Hz (loops at $10)
  4174. ; and update octane if timer is expired (at 2.5Hz)
  4175. ;-----------------------------------------------------------
  4176. brclr Tclocks, #$01, L1227 ; Bail if 40Hz signal no set
  4177. ldab T40s_octane ; b = T40s_octane
  4178. beq L1224 ; Branch if timer expired
  4179. dec T40s_octane ; Update timer
  4180. bne L1227 ; Bail if timer not yet expired
  4181.  
  4182. ;-------------------------------------
  4183. ; T40s_octane expired, update octane
  4184. ;-------------------------------------
  4185. L1224 ldab octane ; b = octane
  4186. tsta ;
  4187. bpl L1225 ; Branch if knockSum >=3 (see carry bit shited-in above)
  4188.  
  4189. ;--------------------------------------------------
  4190. ; knockSum <3, increment octane by 1 (max 255)
  4191. ;--------------------------------------------------
  4192. ldaa #$10 ; pre-load timer value
  4193. incb ; b = octane+1
  4194. bne L1226 ; Branf if no rollover
  4195. decb ; Rollover, use max of 255
  4196. bra L1226 ; Bail to store
  4197.  
  4198. ;--------------------------------------------------
  4199. ; knockSum >=3, decrement octane by 1 (min 0)
  4200. ;--------------------------------------------------
  4201. L1225 ldaa #$10 ; pre-load timer value
  4202. tstb ;
  4203. beq L1226 ; Branch if octane already 0
  4204. decb ; b = octane - 1
  4205. L1226 stab octane ; update octane
  4206. staa T40s_octane ; Re-init T40s_octane with $10
  4207.  
  4208. ;-------------------------------------------------------------------------------------------------
  4209. ; Section to compute timingOct, the timing interpolated from the
  4210. ; two timing maps (timing under high octane and low octane) according
  4211. ; to the current octane value
  4212. ;
  4213. ; timingOct = alpha * t_timingHiOct(rpm, load) + (1-alpha) * t_timingLoOct(rpm, load)
  4214. ;
  4215. ; where alpha = octane/255, 0<= alpha <=1
  4216. ;-------------------------------------------------------------------------------------------------
  4217. ;-------------------------------------------------
  4218. ; Compute rpm and load for 2D map interpolation
  4219. ;-------------------------------------------------
  4220. L1227 ldy #L2052 ;
  4221. jsr rpmPwise ; b = rpm
  4222. stab temp6 ; temp6 = rpm
  4223. jsr getLoadForMaps ; b = load
  4224. stab temp7 ; temp7 = load
  4225.  
  4226. ;--------------------------------------
  4227. ; Get timing value from t_timingHiOct
  4228. ;--------------------------------------
  4229. ldx #t_timingHiOct ; x points to t_timingHiOct
  4230. ldy #$1000 ;
  4231. jsr lookup2D ; b = t_timingHiOct(rpm,load)
  4232. stab timFuelEnr ; timFuelEnr = t_timingHiOct(rpm,load)
  4233.  
  4234. ;------------------------------------------------------------------------------------
  4235. ; Change load value for t_timingLoOct map interpolation (first three row missing)
  4236. ;------------------------------------------------------------------------------------
  4237. ldaa temp7 ; a = load
  4238. suba #$30 ; a = load - $30
  4239. bcs L1229 ; branch if load <$30 (no interpolation, use t_timingHiOct(rpm,load)
  4240. staa temp7 ; temp7 = load-$30
  4241.  
  4242. ;------------------------------------------------------
  4243. ; Compute t_timingHiOct(rpm,load) * octane and put it on stack
  4244. ;------------------------------------------------------
  4245. jsr getOctane ; a = validated octane
  4246. mul ; d = t_timingHiOct(rpm,load) * octane
  4247. psha ; put on stack
  4248. pshb ; put on stack
  4249.  
  4250. ;-------------------------------------
  4251. ; Get timing value from t_timingLoOct
  4252. ;-------------------------------------
  4253. ldx #t_timingLoOct ; x points to t_timingLoOct
  4254. ldy #$1000 ;
  4255. jsr lookup2D ; b = t_timingLoOct(rpm, load)
  4256.  
  4257. ;-----------------------------------------------------------------------------------------------------------
  4258. ; Compute
  4259. ; timingOct = octane/255 * t_timingHiOct(rpm,load) + (255 - octane)/255 * t_timingLoOct(rpm, load)
  4260. ; = alpha * t_timingHiOct(rpm,load) + (1-alpha) * t_timingLoOct(rpm, load)
  4261. ;-----------------------------------------------------------------------------------------------------------
  4262. jsr getOctane ; a = octane
  4263. coma ; a = not(octane) = 255 - octane
  4264. mul ; d = (255 - octane) * t_timingLoOct(rpm, load)
  4265. std temp6 ; temp6:temp7 = (255 - octane) * t_timingLoOct(rpm, load)
  4266. pulb ;
  4267. pula ; d = octane * t_timingHiOct(rpm,load)
  4268. addd temp6 ; d = octane * t_timingHiOct(rpm,load) + (255 - octane) * t_timingLoOct(rpm, load)
  4269. div #$ff ; a = remainder, b = octane/255 * t_timingHiOct(rpm,load) + (255 - octane)/255 * t_timingLoOct(rpm, load)
  4270. rola ; Put remainder high bit in carry
  4271. adcb #$00 ; roundup b with remainder
  4272. L1229 stab timingOct ; timingOct = octane/255 * t_timingHiOct(rpm,load) + (255 - octane)/255 * t_timingLoOct(rpm, load)
  4273.  
  4274. ;--------------------------------------------------------------------------------------------------
  4275. ; Compute timFuelEnr = $4b/256 * ($b6/64 * (t_timingHiOct(rpm,load) - timingOct) + knockSum - 5)
  4276. ; timFuelEnr is a fuel enrichment based on timing, octane and knockSum
  4277. ;--------------------------------------------------------------------------------------------------
  4278. ldaa timFuelEnr ; a = t_timingHiOct(rpm,load)
  4279. suba timingOct ; a = t_timingHiOct(rpm,load) - timingOct
  4280. bcc L1230 ; Branch if result positive
  4281. clra ; Use min of 0
  4282. L1230 ldab #$b6 ; b = $b6
  4283. mul ; d = $b6 * (t_timingHiOct(rpm,load) - timingOct)
  4284. jsr scale64 ; b = $b6/64 * (t_timingHiOct(rpm,load) - timingOct)
  4285. addb knockSum ; b = $b6/64 * (t_timingHiOct(rpm,load) - timingOct) + knockSum
  4286. bcc L1232 ; Branch if no overflow
  4287. ldab #$ff ; Overflow, use max of 255
  4288. L1232 subb #$05 ; b = $b6/64 * (t_timingHiOct(rpm,load) - timingOct) + knockSum - 5
  4289. bcc L1233 ; Branch if result positive
  4290. clrb ; Use min of 0
  4291. #ifdef E931
  4292. L1233 ldaa #$4b ; a = $4b
  4293. #else
  4294. L1233 ldaa #$86 ; a = $86
  4295. #endif
  4296. mul ; d = $4b*($b6/64 * (t_timingHiOct(rpm,load) - timingOct) + knockSum - 5)
  4297. staa timFuelEnr ; timFuelEnr = $4b/256 * ($b6/64 * (t_timingHiOct(rpm,load) - timingOct) + knockSum - 5)
  4298.  
  4299. ;---------------------------------------
  4300. ; Section to update maxAdv for E931
  4301. ;---------------------------------------
  4302. #ifdef E931
  4303. brset state1, #$04, Mdc7a ; Branch if engine runningFast
  4304. brset state1, #$10, Mdc76 ; Branch if notRotating
  4305. ldaa rpm8 ;
  4306. cmpa t_idleSpd ;
  4307. bcs Mdc76 ; Branch if rpm8 < t_idleSpd (normal idle speed)
  4308. ldaa tpsDiffMax2 ;
  4309. cmpa #$03 ;
  4310. bcc Mdc76 ; Branch if tpsDiffMax2 >= 3 (pedal is moving forward...)
  4311. ldaa airDiffPos1 ;
  4312. cmpa #$0a ;
  4313. bcs L1234 ; Bail (dont even update maxAdv) if airDiffPos1 < $0a (airflow decrease or small airflow increase)
  4314.  
  4315. ;----------------------------------------
  4316. ; Use max of $80 (maxAdv=$80, no limit?)
  4317. ; if notRotating
  4318. ; or rpm8 < t_idleSpd
  4319. ; or tpsDiffMax2 >= 3
  4320. ; or airDiffPos1 >= $0a
  4321. ;----------------------------------------
  4322. Mdc76 ldaa #$80 ; Use default of $80 (no limit)
  4323. bra Mdc90 ; Branch to store
  4324.  
  4325. ;--------------------------------------------------------------------
  4326. ; Engine runningFast, compute maxAdv which will reduce the
  4327. ; timing advance by 13deg or limit it to 12deg
  4328. ;---------------------------------------------------------------------
  4329. Mdc7a ldaa vssCnt1 ;
  4330. beq L1234 ; Bail if car not moving
  4331. ldaa #$05 ;
  4332. staa T_maxAdv ; Init timer T_maxAdv = 5 ?
  4333. ldaa timingOct ;
  4334. suba #$0d ;
  4335. bcc Mdc8a ; Branch if timingOct - $0d positive
  4336. clra ; Use min of 0
  4337. Mdc8a ldab #$12 ; b = $12
  4338. cba ;
  4339. bcc Mdc90 ; Branch if timingOct-$0d >= $12
  4340. tba ; Use max of $12 degrees
  4341. Mdc90 staa maxAdv ; maxAdv = max(timingOct - $0d, 12)
  4342. #endif
  4343.  
  4344. ;-------------------------------------------------
  4345. ; Set timAdjFlags.1 flag if rpm>2000rpm or reset it
  4346. ;-------------------------------------------------
  4347. L1234 ldab #$40 ; assume b = threshold = 2000rpm
  4348. brclr timAdjFlags, #$01, L1235 ; Branch if flag clear
  4349. ldab #$3a ; Flag was previously set, use a lower threshold of 1813rpm (hysteresis)
  4350. L1235 andm timAdjFlags, #$fe ; Reset flag
  4351. cmpb rpm31 ;
  4352. bcc L1237 ; Branch if rpm31 <= threshold (2000rpm or 1813rpm)
  4353. orm timAdjFlags, #$01 ; rpm31 > threshold, set flag
  4354.  
  4355. ;------------------------------------------------------------------------
  4356. ; Compute advRpm under some conditions (low rpm, idle switch off, etc.)
  4357. ; advRpm is an rpm based timing advance/retard with +/-8deg max
  4358. ;------------------------------------------------------------------------
  4359. L1237 ldaa #$80 ; preload a = $80 = default value (no timing change)
  4360. brset timAdjFlags, #$01, L1243 ; Bail if rpm31 > 2000rpm (with hysteresis)
  4361. brset iscFlags0, #$04, L1243 ; Bail if basic idle speed adjustment mode is active (no timing change)
  4362. brset state1, #$19, L1243 ; Bail if notRotating or startingToCrank or rotatingStopInj
  4363. brclr port3Snap0, #$80, L1243 ; Bail if idle switch on
  4364. ldab vssCnt1 ;
  4365. brn L1243 ; Branch never (?)
  4366. ldx #$0000 ; x = 0
  4367. clrb ; b = 0
  4368. ldaa idleSpdTarg ; d = 256*idleSpdTarg
  4369. #ifdef E931 ;
  4370. suba #$06 ; d = 256*(idleSpdTarg - 6) (-47rpm)
  4371. #else ;
  4372. suba #$04 ;
  4373. #endif ;
  4374. bcc L1238 ; Branch if no underflow
  4375. clra ; Underflow, use min of 0
  4376. L1238 jsr scale8 ; a = 256/8 * (idleSpdTarg - 6) = 32*(idleSpdTarg - 6)
  4377. cmpd rpmX4Filt ;
  4378. bcc L1239 ; Branch if 32*(idleSpdTarg - 6) >= rpmX4Filt
  4379. ldd rpmX4Filt ; use min of rpmX4Filt
  4380. L1239 jsr scale16 ; d = 2*(idleSpdTarg - 6)
  4381. subd rpm4 ; d = 2*(idleSpdTarg - 6) - rpm4
  4382. bcc L1240 ; Branch if positive
  4383. inx ; result negative, x = 1
  4384. coma ;
  4385. comb ;
  4386. addd #$0001 ; d = rpm4 - 2*(idleSpdTarg - 6)
  4387. L1240 jsr ovfCheck ; b = result = abs(rpm4 - 2*(idleSpdTarg - 6))
  4388. ldaa #$68 ; a = $68
  4389. mul ; d = $68 * abs(rpm4 - 2*(idleSpdTarg - 6))
  4390. jsr round256 ; a = $68/256 * abs(rpm4 - 2*(idleSpdTarg - 6))
  4391. cmpa #$08 ;
  4392. bls L1241 ; Branch if a <= 8 (+/-8 degrees advance or retard)
  4393. ldaa #$08 ; Use max of 8
  4394. L1241 dex ; x = x-1
  4395. bne L1242 ; Branch if we did not have a negative result earlier
  4396. nega ; Result was negative, negate again to restore it...
  4397. L1242 adda #$80 ; a = $80 + $68/256 * (rpm4 - 2*(idleSpdTarg - 6))
  4398. L1243 staa advRpm ; advRpm = $80 + $68/256 * (rpm4 - 2*(idleSpdTarg - 6))
  4399.  
  4400. ;--------------------------------------------------
  4401. ; Compute advEct, ect based timing advance/retard
  4402. ;--------------------------------------------------
  4403. ldx #L2020
  4404. jsr interpEct
  4405. stab advEct ; advEct = L2020(ect)
  4406.  
  4407. ;---------------------------------------------------------------
  4408. ; Compute advIat iat based advance/retard under low load
  4409. ;---------------------------------------------------------------
  4410. ldx #L2038 ;
  4411. jsr interp16rpm ; b = L2038(rpm)
  4412. cmpa airVolB ;
  4413. bls L1244 ; Branch if L2038(rpm) <= airVolB
  4414. ldaa #$80 ; Use default value of $80 under high load
  4415. bra L1245 ;
  4416. L1244 ldx #L2021 ; L2038(rpm) <= airVolB
  4417. jsr iatCInterp ;
  4418. L1245 staa advIat ; advIat = L2021(iat)
  4419.  
  4420. ;----------------------------------------------------------------------------------
  4421. ; Compute advTotal = min(timingOct, maxAdv) + advEct + advIat + advRpm - $0180
  4422. ; = min(timingOct, maxAdv) + (advEct-$80) + (advIat-$80) + (advRpm-$80)
  4423. ;----------------------------------------------------------------------------------
  4424. ldx #advTotal ; x points to advTotal (table of timing related value)
  4425. ldab $01,x ; b = timingOct (timing corrected for current octane)
  4426. #ifdef E931
  4427. cmpb maxAdv ;
  4428. bls L1246 ; branch if timingOct<=maxAdv
  4429. ldab maxAdv ; Use max of maxAdv
  4430. #endif
  4431. L1246 clra ; d = min(timingOct, maxAdv) (a = 0...)
  4432. xgdy ; y = min(timingOct, maxAdv)
  4433. ldab $02,x ; b = advEct
  4434. aby ; y = min(timingOct, maxAdv) + advEct
  4435. ldab $03,x ; b = advIat
  4436. aby ; y = min(timingOct, maxAdv) + advEct + advIat
  4437. ldab $04,x ; b = advRpm
  4438. aby ; y = min(timingOct, maxAdv) + advEct + advIat + advRpm
  4439. xgdy ; d = min(timingOct, maxAdv) + advEct + advIat + advRpm
  4440. cmpd #$01bc ;
  4441. bls L1247 ; Branch if min(timingOct, maxAdv) + advEct + advIat + advRpm <= $01bc
  4442. ldd #$01bc ; Use max of $01bc
  4443.  
  4444. L1247 cmpd #$0180 ;
  4445. bcc L1248 ; Branch if min(timingOct, maxAdv) + advEct + advIat + advRpm >= $0180
  4446. ldd #$0180 ; Use min of $0180
  4447.  
  4448. L1248 subd #$0180 ; d = min(timingOct, maxAdv) + advEct + advIat + advRpm - $0180
  4449. stab $00,x ; advTotal = min(timingOct, maxAdv) + advEct + advIat + advRpm - $0180
  4450.  
  4451. ;----------------------------------------------------------
  4452. ; Compute tim61Tot0 = $e7 - $b6/64 * (advTotal + $0a)
  4453. ; = 256 * (61deg - (advTotal-10deg)) / 90
  4454. ;
  4455. ; This is timing referenced to -61deg BTDC
  4456. ; -10deg is because advTotal is shifted by 10deg (timingOct
  4457. ; is from the timing maps which are shifted by 10 deg...)
  4458. ;----------------------------------------------------------
  4459. addb #$0a ; b = advTotal + $0a
  4460. ldaa #$b6 ; a = $b6
  4461. mul ; d = $b6 * (advTotal + $0a)
  4462. jsr scale64 ; d = $b6/64 * (advTotal + $0a)
  4463. ldaa #$e7 ; a = $e7
  4464. sba ; a = $e7 - $b6/64 * (advTotal + $0a)
  4465. staa tim61Tot0 ; tim61Tot0 = $e7 - $b6/64 * (advTotal + $0a)
  4466.  
  4467. ;-------------------------------
  4468. ; Compute timingAdv from tim61
  4469. ;-------------------------------
  4470. ldaa #$a0 ; a = $a0 (default when engine not running)
  4471. brset state1, #$11, L1249 ; Branch if notRotating or startingToCrank
  4472. ldaa tim61 ; a = tim61
  4473. L1249 ldab #$5a ; b = $5a
  4474. mul ; d = $5a * tim61
  4475. jsr round256 ; a = $5a * tim61/256
  4476. nega ; a = -($5a * tim61/256)
  4477. adda #$47 ; a = -($5a * tim61/256) + $47 = $47 - $5a * tim61/256
  4478. staa timingAdv ; timingAdv = $147 - $5a * tim61/256
  4479.  
  4480. ;-------------------
  4481. ; Compute enerLen
  4482. ;-------------------
  4483. ldaa battRaw ;
  4484. suba #$80 ; a = battRaw - $80 (9.38v)
  4485. bcc L1250 ; branch if underflow
  4486. clra ; Use min of 0
  4487. L1250 staa temp5 ; temp5 = max(0,battRaw - $80)
  4488. ldx #t_enerLen ; x points to t_enerLen
  4489. jsr interp16b ; b = t_enerLen(battRaw)
  4490. stab enerLen ; enerLen = t_enerLen(battRaw)
  4491.  
  4492. ;-------------------------------------------------
  4493. ; Update coilChkFlags.5 flag, Set bit if engine
  4494. ; running and rpm<5000 and 8V<=battRaw<=18V
  4495. ;-------------------------------------------------
  4496. brset state1, #$11, L1253 ; Branch if notRotating or startingToCrank
  4497. ldaa rpm31 ; a = rpm31
  4498. cmpa #$a0 ;
  4499. bcc L1253 ; Branch if rpm31 >= 5000rpm
  4500. ldaa battRaw ; a = battRaw
  4501. cmpa #$f5 ; 18V
  4502. bhi L1253 ; Branch if battRaw > 18V
  4503. cmpa #$6d ; 8V
  4504. bcs L1253 ; Branch if battRaw < 8V
  4505. orm coilChkFlags, #$20 ; At this point voltage is between 8V and 18V, set bit
  4506. bra L1255 ;
  4507. L1253 andm coilChkFlags, #$df ; Reset bit
  4508.  
  4509. ;------
  4510. ; Exit
  4511. ;------
  4512. L1255 rts ;
  4513.  
  4514.  
  4515.  
  4516. ;******************************************************************
  4517. ;
  4518. ; Return the octane value if sensors look ok, 0 otherwise
  4519. ; (very low octane to be on the safe side)
  4520. ;
  4521. ;
  4522. ;
  4523. ;******************************************************************
  4524. getOctane clra
  4525. brset state2, #$28, L1257 ; Branch if no pulse accumulator interrupts or knock sensor not working
  4526. ldaa octane
  4527. L1257 rts
  4528.  
  4529.  
  4530.  
  4531. ;******************************************************************
  4532. ;
  4533. ;
  4534. ; Third subroutine
  4535. ;
  4536. ;
  4537. ;******************************************************************
  4538. ;-------------------------------------------
  4539. ; Update fuel pump activation/deactivation
  4540. ;-------------------------------------------
  4541. subroutine3 brclr state1, #$10, L1259 ; Branch if notRotating clear
  4542. brset obdActCmd, #$02, L1259 ; Engine notRotating, branch if fuel pump is being actuated through OBD command
  4543. orm port1, #$10 ; De-activate fuel pump relay
  4544. bra L1260 ; Bail
  4545. L1259 andm port1, #$ef ; activate fuel pump relay
  4546.  
  4547. ;------------------------------------------------------------------------
  4548. ; Re-init T40_acOn if A/C switch on is off
  4549. ; (implement a min delay before turning A/C on after button is pressed)
  4550. ;------------------------------------------------------------------------
  4551. L1260 ldx #T40_acOn ; X points to 40Hz timer T40_acOn
  4552. brclr port3Snap0, #$10, L1261 ; Branch if A/C switch flag off (switch is on?)
  4553. ldaa #$18 ; Switch is off, init timer
  4554. staa $00,x ; Re-init T40_acOn with $18 (0.6s, min time before activating A/C)
  4555.  
  4556. ;---------------------------------------------------------------------------------
  4557. ; Re-init T40_acOnRpm if rpm<438
  4558. ; (implement a min delay before turning A/C on after RPM > 438 (after start-up)
  4559. ;---------------------------------------------------------------------------------
  4560. L1261 ldaa rpm31 ; a = rpm
  4561. cmpa #$0e ; 438rpm
  4562. bhi L1262 ; Branch if rpm > 438
  4563. ldaa #$20 ; rpm lower than 438, init timer
  4564. staa $01,x ; rpm<438 -> Re-init timer #T40_acOnRpm with $20 (0.8s)
  4565.  
  4566. ;--------------------------------------------------------------
  4567. ; For AT, decide if we will turn A/C on/off based on TPS...
  4568. ;--------------------------------------------------------------
  4569. L1262 .equ $ ;
  4570. #ifdef E932
  4571. ;-----------------------------------------------------------------
  4572. ; Load TPS value of 78% or 82% (80% target with +/-2% hysteresis)
  4573. ;-----------------------------------------------------------------
  4574. ldaa #$d2 ; Load 82% threshold
  4575. brclr varFlags0, #$01, L1263 ; Do not branch if TPS was higher than 82% the previous time we were here
  4576. ldaa #$c7 ; Use 78% threshold instead (hysteresis)
  4577.  
  4578. ;-----------------------------------------------------------------
  4579. ; Check if TPS is above/below threshold and set varFlags0 accordingly
  4580. ;-----------------------------------------------------------------
  4581. L1263 orm varFlags0, #$01 ; Assume this bit will be set (reset below)
  4582. cmpa tpsRaw ;
  4583. bls L1264 ; Branch if threshold smaller than TPS (TPS higher than threshold)
  4584.  
  4585. ;----------------------------------------------------------------------------------
  4586. ; TPS lower than threshold, reset varFlags0.0 flag and set timer T40_acCut to 5s
  4587. ;----------------------------------------------------------------------------------
  4588. andm varFlags0, #$fe ; TPS lower than threshold, reset varFlags0.0
  4589. ldaa #$c8 ; 5s at 40Hz
  4590. staa $02,x ; Init T40_acCut to 5s (delay before turning A/C back on)
  4591. #endif
  4592.  
  4593. ;---------------------------------------------------------------------------
  4594. ; Make sure both T40_acOn and T40_acOnRpm are 0 before attempting to turn A/C on
  4595. ; Implement a min delay before engaging A/C
  4596. ; clutch once car has started or A/C button is pressed
  4597. ;---------------------------------------------------------------------------
  4598. L1264 ldaa $00,x ; a = T40_acOn
  4599. oraa $01,x ; a = T40_acOn | T40_acOnRpm
  4600. bne L1266 ; Branch if at least one timert not yet 0 (turn A/C off)
  4601.  
  4602. ;-----------------------------------------------------------------------------
  4603. ; At this point, both T40_acOn and T40_acOnRpm timers are at 0, we can turn A/C on
  4604. ;-----------------------------------------------------------------------------
  4605. #ifdef E932
  4606. ;--------------------------------------------------------------
  4607. ; For AT, decide if we will turn A/C on/off based on TPS...
  4608. ; Seems A/C is cutoff for a maximum of 5 seconds when TPS>80%
  4609. ; but it is turned back on whenever TPS goes below 80% (no delay)
  4610. ; It would probably be better to turn-it off for 5s anyway...
  4611. ;--------------------------------------------------------------
  4612. brclr varFlags0, #$01, L1265 ; Branch if TPS was lower than threshold (turn it back on immediately, not the best????)
  4613. brset port3Snap0, #$20, L1265 ; TPS is higher than threshold, branch if Park/neutral flag is set (no need to cutoff if in park)
  4614. ldaa $02,x ; Get T40_acCut value to see if we can turn A/C back on (5 second delay)
  4615. bne L1266 ; Branch if timer not expired
  4616. #endif
  4617.  
  4618. ;----------------
  4619. ; Turn A/C on
  4620. ;----------------
  4621. L1265 andm port1, #$df ; Turn A/C clutch bit to 0
  4622. bra L1267
  4623.  
  4624. ;----------------
  4625. ; Turn A/C off
  4626. ;----------------
  4627. L1266 orm port1, #$20 ; Turn A/C clutch bit to 1
  4628.  
  4629. ;--------------------------------------------------------------
  4630. ; Section to update the purge solenoid activation/deactivation
  4631. ;--------------------------------------------------------------
  4632. ;------------------------------------------------------------
  4633. ; Reset forced activation and forced deactivation flags
  4634. ; since we are going to update them
  4635. ;------------------------------------------------------------
  4636. L1267 andm varFlags0, #$9f ; Reset bits 01100000 ($20 and $40)
  4637.  
  4638. ;-----------------------------------
  4639. ; Branch according to engine state
  4640. ;-----------------------------------
  4641. brclr state1, #$10, L1270 ; Branch if notRotating clear
  4642.  
  4643. ;-----------------------------------------------------------
  4644. ; Engine notRotating, check if an OBD command is ongoing
  4645. ; to activate solenoid, set flags in consequence
  4646. ;-----------------------------------------------------------
  4647. brset obdActCmd, #$01, L1269 ; Branch if purge solenoid is actuated by OBD
  4648. orm varFlags0, #$20 ; Set "forced deactivation flag"
  4649. bra L1276 ; Branch to continue
  4650. L1269 orm varFlags0, #$40 ; Set "forced activation" flag
  4651. bra L1276 ; Branch to reset pulsewidth modulation flag since engine not rotating...
  4652.  
  4653. ;----------------------------------------------------------------
  4654. ; Engine rotating
  4655. ; Check if minimum conditions are met to activate purge solenoid
  4656. ;----------------------------------------------------------------
  4657. L1270 ldaa T2_crank ; a = T2_crank
  4658. adda #$78 ;
  4659. bcs L1271 ; Branch if engine stopped "startingToCrank" less than 60 sec ago.
  4660. brclr ftrimFlags, #$03, L1271 ; Branch if current trim range is "low"
  4661. ldaa ectFiltered ; a = ectFiltered
  4662. cmpa #$2d ; 66degC
  4663. bls L1272 ; Branch if temperature(ectFiltered) >= 66degC
  4664.  
  4665. ;-----------------------------------------------------------
  4666. ; Conditions are not good to activate purge solenoid
  4667. ; Set "forced deactivation" flag
  4668. ;-----------------------------------------------------------
  4669. L1271 orm varFlags0, #$20 ; Set flag
  4670. bra L1274 ; Branch to continue
  4671.  
  4672. ;---------------------------------------------------------------
  4673. ; Minimum condition for activation are met, check if there are
  4674. ; special conditions where we should always activate???
  4675. ;---------------------------------------------------------------
  4676. L1272 brclr state1, #$80, L1273 ; Branch to activate if open loop mode is active
  4677.  
  4678. ;------------------------------
  4679. ; We are in closed loop mode
  4680. ;------------------------------
  4681. ldaa baroChecked ; a = baroChecked
  4682. cmpa #$9c ; 1 bar
  4683. bcs L1273 ; Branch to activate if baroChecked < 0.76 bar, activate if baro is very low?
  4684. ldaa iatChecked ; a = iatChecked
  4685. cmpa #$49 ; 50degC
  4686. bhi L1274 ; Branch if temperature(iatChecked) < 50degC
  4687.  
  4688. ;--------------------------------------------------------------------------------
  4689. ; At this point, min conditions are met and either
  4690. ; open loop mode is active
  4691. ; or
  4692. ; closed loop mode is active and (baroChecked < 0.76 or temperature(iatChecked) >= 50degC)
  4693. ;
  4694. ; Set "forced activation" flag indicating
  4695. ; we should activate purge solenoid
  4696. ;--------------------------------------------------------------------------------
  4697. L1273 orm varFlags0, #$40 ; Set flag
  4698.  
  4699. ;----------------------------------------------------------------
  4700. ; Continuation from code flows above when engine is rotating...
  4701. ; Update varFlags0.7 deactivation flag if its timer is expired
  4702. ;
  4703. ; varFlags0.7 deactivation flag is used to activate/deactivate
  4704. ; solenoid when none of the other two flags are set. Flag
  4705. ; stays set for 24sec and stays reset for 212 sec. Toggled
  4706. ; between the two states, basically implementing pulsewidth
  4707. ; modulation with a very long period...
  4708. ;----------------------------------------------------------------
  4709. L1274 ldaa T0p5_purge ; a = T0p5_purge
  4710. bne L1278 ; Bail to activate/deactivate if timer not expired
  4711.  
  4712. ;-----------------------------------------------------------------
  4713. ; Timer is expired, time to toggle the flag,
  4714. ; branch to appropriate section depending on current flag value
  4715. ;-----------------------------------------------------------------
  4716. brset varFlags0, #$80, L1275 ; Branch if deactivation flag was set previously
  4717.  
  4718. ;-----------------------------------------------------------------------
  4719. ; Flag is not set, first check if "forced activation" is requested
  4720. ; in that case we just bail since it doesn't matter anymore...
  4721. ;-----------------------------------------------------------------------
  4722. brset varFlags0, #$40, L1278 ; Bail to activate if solenoid needs "forced activation"
  4723.  
  4724. ;-----------------------------------------------------------------------
  4725. ; Time has come to toggle the flag to 1 and reset the timer to 24sec
  4726. ;-----------------------------------------------------------------------
  4727. ldaa #$0c ; a = 24s
  4728. orm varFlags0, #$80 ; Set flag
  4729. bra L1277 ; Branch to update timer and activate/deactivate
  4730.  
  4731. ;-----------------------------------------------------------------------
  4732. ; Flag is set, first check if "forced deactivation" is requested
  4733. ; in that case we just bail since it doesn't matter anymore...
  4734. ;-----------------------------------------------------------------------
  4735. L1275 brset varFlags0, #$20, L1278 ; Bail to activate/deactivate if "forced deactivation" flag is set
  4736.  
  4737. ;-----------------------------------------------------------------------
  4738. ; Time has come to toggle the flag to 0 and reset the timer to 212sec
  4739. ;-----------------------------------------------------------------------
  4740. L1276 ldaa #$6a ; 212s
  4741. andm varFlags0, #$7f ; Reset flag
  4742. L1277 staa T0p5_purge ; T0p5_purge = 212sec
  4743.  
  4744. ;------------------------------------------------------------------
  4745. ; Continuation from all code flows above...
  4746. ; Based on flags, decide to activate or deactivate purge solenoid
  4747. ; Flags are tested in priority order...
  4748. ;------------------------------------------------------------------
  4749. L1278 brset varFlags0, #$40, L1279 ; 1st priority, branch to activate if forced activation is set
  4750. brset varFlags0, #$20, L1280 ; 2nd priority, branch to deactivate if forced deactivation is set
  4751. brset varFlags0, #$80, L1280 ; 3rd priority, branch to deactivate if pulswidth modulation flag is set
  4752.  
  4753. ;---------------------------
  4754. ; Activate purge solenoid
  4755. ;---------------------------
  4756. L1279 andm port6, #$ef ; Activate purge solenoid
  4757. bra L1281 ;
  4758.  
  4759. ;----------------------------
  4760. ; Deactivate purge solenoid
  4761. ;----------------------------
  4762. L1280 orm port6, #$10 ; Deactivate purge solenoid
  4763.  
  4764.  
  4765. ;--------------------------------------------------------------
  4766. ; Section to update the EGR solenoid activation/deactivation
  4767. ;--------------------------------------------------------------
  4768. ;----------------------------------------------------------------
  4769. ; Compute egr duty cycle factor
  4770. ; as a function of rpm and airVol from 2D table t_egrDutyFact
  4771. ;----------------------------------------------------------------
  4772. L1281 ldab rpmIndex1 ;
  4773. ldaa #$70 ; max of rpm
  4774. jsr abmin ; b = max(rpmIndex1, $70)
  4775. stab temp6 ; column index is rpm
  4776. ldab airVol ;
  4777. ldaa #$80 ; max of airVol
  4778. jsr rpmRange ;
  4779. stab temp7 ; row index is airVol
  4780. ldab #$80 ; b = 100% duty cycle
  4781. brclr state1, #$10, L1282 ; Branch if notRotating clear
  4782. brset obdActCmd, #$08, L1283 ; Engine notRotating, branch if EGR solenoid actuated (by OBD) -> use 100% duty
  4783. L1282 clrb ; b = 0% duty cycle
  4784. brset state2, #$08, L1283 ; Branch if no pulse accumulator interrupts -> use 0% duty cycle
  4785. brset state1, #$11, L1283 ; Branch if notRotating or startingToCrank -> use 0% duty cycle
  4786. ldx #t_egrDutyFact ;
  4787. ldy #$0800 ;
  4788. jsr lookup2D ; b = t_egrDutyFact(rpm, airVol)
  4789. clra ;
  4790. std temp6 ; temp6:temp7 = t_egrDutyFact(rpm,airVol)
  4791.  
  4792. ;---------------------------------------------------------------------------------
  4793. ; Get EGR solenoid duty cycle from t_egrDuty and apply factor from above
  4794. ;---------------------------------------------------------------------------------
  4795. ldx #t_egrDuty ;
  4796. ldaa ectCond ;
  4797. jsr interp32mul ; b = t_egrDutyFact(rpm,airVol) * t_egrDuty(ect)
  4798. L1283 stab egrDuty128 ; egrDuty128 = t_egrDutyFact(rpm,airVol) * t_egrDuty(ect) with $80=100%
  4799.  
  4800. ;-------------------------------------
  4801. ; Scale duty factor to 00 - $30 range
  4802. ;-------------------------------------
  4803. ldaa #$30 ;
  4804. mul ;
  4805. jsr scale128 ; b = $30 * egrDuty128/128
  4806. stab egrDuty ; egrDuty with max of $30=100%
  4807.  
  4808. ;--------------------------------------------------------------------
  4809. ; Re-Init T2_EcuPower to $ff if T40_noPower expired
  4810. ; (T2_EcuPower will start counting from $ff when power is back on...
  4811. ;--------------------------------------------------------------------
  4812. ldaa T40_noPower ;
  4813. bne L1284 ; Branch if ECU still has power
  4814. ldaa #$ff ; ECU about to loose power, reset T2_EcuPower to max (127.5sec)
  4815. staa T2_EcuPower ;
  4816.  
  4817. ;-----------------------------------------------------------
  4818. ; Section to update boost control solenoid duty cycle
  4819. ;-----------------------------------------------------------
  4820. ;-----------------------------------------------------------
  4821. ; Check if time has come, section is updated at ~40Hz
  4822. ;-----------------------------------------------------------
  4823. L1284 brclr Tclocks, #$01, L1298 ; Bail of section if 40Hz signal no set
  4824. ldab fpsBcsFlags ; b = old fpsBcsFlags
  4825. andm fpsBcsFlags, #$8f ; Assume those three flags are reset, updated below (0111 0000)
  4826.  
  4827. ;-------------------------------------------------------------------
  4828. ; Check if octane is above/below threshold with hysteresis
  4829. ; (high $c0, low $9a) and update fpsBcsFlags.5 flag (reset above)
  4830. ;-------------------------------------------------------------------
  4831. ldaa #$9a ; start with low threshold, a = $9a
  4832. bitb #$20 ; test bit
  4833. bne L1285 ; Branch if old fpsBcsFlags.5 was set
  4834. ldaa #$c0 ; bit was not set, use higher threshold a = $c0
  4835. L1285 cmpa octane ;
  4836. bcc L1286 ; Branch if octane <= threshold
  4837. orm fpsBcsFlags, #$20 ; set flag since we are above threshold
  4838.  
  4839. ;-------------------------------------------------------------------
  4840. ; Check if mafRaw16 is above/below threshold with hysteresis
  4841. ; (high $4e, low $38) and update fpsBcsFlags.4 flag (reset above)
  4842. ;-------------------------------------------------------------------
  4843. L1286 ldaa #$38 ; a = #0038
  4844. bitb #$10 ;
  4845. bne L1287 ; Branch if old fpsBcsFlags.4 was set
  4846. #ifdef E931
  4847. ldaa #$4e ; a = #004e
  4848. #else
  4849. ldaa #$4a ;
  4850. #endif
  4851. L1287 cmpa mafRaw16 ;
  4852. bcc L1288 ; branch if mafRaw16 <= threshold
  4853. orm fpsBcsFlags, #$10 ; set flag since we are above threshold
  4854.  
  4855.  
  4856. ;-------------------------------------------------
  4857. ; Branch to proper section if engine not running
  4858. ;-------------------------------------------------
  4859. L1288 brclr state1, #$10, L1289 ; Branch if notRotating clear
  4860. brset obdActCmd, #$20, L1295 ; Engine notRotating, branch if boost solenoid actuated (by OBD command I assume)
  4861. L1289 brset state1, #$11, L1293 ; branch if notRotating or startingToCrank
  4862.  
  4863. ;----------------------------------------------------
  4864. ; Engine is running...
  4865. ; Check if time has come to update
  4866. ;----------------------------------------------------
  4867. ldaa T40s_bcs ;
  4868. bne L1297 ; Branch if timer not expired
  4869.  
  4870. ;----------------------------------------------------
  4871. ; Timer expired, time to update has come (~2Hz)
  4872. ;
  4873. ; By default, increase bcsDuty by 8 and test if we
  4874. ; should not reduce it instead
  4875. ;----------------------------------------------------
  4876. ldab bcsDuty ; b = bcsDuty
  4877. addb #$08 ; b = bcsDuty + 8
  4878. brset state2, #$28, L1292 ; Branch if no pulse accumulator interrupts being received or knock sensor not working
  4879. brset fpsBcsFlags, #$10, L1292 ; Branch if mafRaw16 above threshold
  4880. brset fpsBcsFlags, #$20, L1294 ; Branch if octane above threshold
  4881.  
  4882. ;--------------------------------------------------------------
  4883. ; At this point, knock sensor is not working or we are not
  4884. ; receiving airflow sensor interrupts or mafRaw16 is above
  4885. ; threshold or octane is below threshold, basically these
  4886. ; are onditions where we would want to reduce turbo pressure
  4887. ;
  4888. ; Reduce bcsDuty by 2 instead of increasing by 8
  4889. ;--------------------------------------------------------------
  4890. L1292 subb #$10 ; b = bcsDuty + 8 - 10 = bcsDuty - 2
  4891. bcc L1294 ; Branch if no underflow
  4892. L1293 clrb ; Use min of 0
  4893.  
  4894. ;---------------------------------------
  4895. ; Check new bcsDuty for max of $30,
  4896. ; store new value and update the timer
  4897. ;---------------------------------------
  4898. L1294 cmpb #$30 ;
  4899. bcs L1296 ; Branch if new bcsDuty < $30
  4900. L1295 ldab #$30 ; Use max of $30
  4901. L1296 stab bcsDuty ; Store new bcsDuty
  4902. ldaa #$14 ; Re-init timer to 20 (0.5sec)
  4903. L1297 deca ; Decrement timer
  4904. staa T40s_bcs ; Store updated timer
  4905.  
  4906.  
  4907. ;-------------------------------------
  4908. ; Decrement T40s_tps at 40Hz
  4909. ;-------------------------------------
  4910. L1298 sei ;
  4911. ldx #T40s_tps ;
  4912. jsr decX40Hz ; Decrement T40s_tps at 40Hz
  4913. cli ;
  4914.  
  4915. ;------------------------------------
  4916. ; Compute the boost gauge duty cycle
  4917. ; depending on current conditions
  4918. ;------------------------------------
  4919. clrb ; load default duty cycle of b = 0
  4920. ldaa T40_noPower ;
  4921. beq L1299 ; Bail if timer expired (ECU is about to shut-down...) (use 0 duty)
  4922. ldab #$0c ; Load default duty cycle of b = $0c
  4923. brset state1, #$10, L1299 ; Bail if notRotating (use half duty...)
  4924. clrb ; load default duty cycle of b = 0
  4925. brset state2, #$08, L1299 ; Bail if no pulse accumulator interrupts (use 0 duty)
  4926. ldx #t_bGauge ; x points to boost gauge table
  4927. ldaa airVolT ; a = airVolT
  4928. #ifdef batteryGauge
  4929. jsr battGauge ;
  4930. #else
  4931. jsr interp32 ; b = t_bGauge(airVolT) (max value is $18...)
  4932. #endif
  4933. ;-------------------------------------------------
  4934. ; Update bGaugeODuty with $18-dutyCycle (min of 0)
  4935. ; bGaugeODuty is the off-duty cycle...
  4936. ;-------------------------------------------------
  4937. L1299 ldaa #$18 ; a = $18
  4938. sba ; a = $18-t_bGauge(airVolT)
  4939. bcc L1300 ; Branch if no underflow
  4940. clra ; underflow, use min
  4941. L1300 staa bGaugeODuty ; Update boost gauge off-duty cycle
  4942.  
  4943. ;----------------------------------------------
  4944. ; Section to update the fuel pressure solenoid
  4945. ;----------------------------------------------
  4946. ;-----------------------------------------------------
  4947. ; First check if there are any reason to activate it
  4948. ;-----------------------------------------------------
  4949. ldab fpsBcsFlags ; b = old fpsBcsFlags, used later...
  4950. andm fpsBcsFlags, #$fb ; Reset bit indicating solenoid was just deactivated will be updated below if required
  4951. brclr state1, #$11, L1302 ; branch if notRotating and startingToCrank clear
  4952. andm fpsBcsFlags, #$f7 ; engine is either notRotating or startingToCrank
  4953. brclr state1, #$10, L1301 ; Branch if notRotating clear (startingToCrank is set...)
  4954. brset obdActCmd, #$04, L1306 ; branch to activate solenoid if OBD command activated
  4955. bra L1307 ; No reason to activate it, branch to deactivate solenoid
  4956.  
  4957. ;-----------------------------------------------------------------
  4958. ; Engine is startingToCrank, check if we should set fpsBcsFlags.3 flag (vapor lock)
  4959. ; Basically set the flag when vapor lock conditions exists
  4960. ;-----------------------------------------------------------------
  4961. L1301 ldaa iatChecked ;
  4962. cmpa #$9d ;
  4963. bhi L1302 ; Branch if temperature(iatChecked) < 10degC
  4964. ldaa ectFiltered ;
  4965. cmpa #$27 ; 72degC
  4966. bhi L1302 ; Branch if temperature(ectFiltered) < 72degC
  4967.  
  4968. ;------------------------------------------------------------------------------------------
  4969. ; At this point engine is startingToCrank and temperature(iatChecked) >= 10degC
  4970. ; and temperature(ectFiltered) >= 72degC, set flag indicating vapor lock conditions exist, NOT???
  4971. ;------------------------------------------------------------------------------------------
  4972. orm fpsBcsFlags, #$08 ; Set flag indicating vapor lock conditions exist
  4973.  
  4974. ;--------------------------------------------------------------------
  4975. ; Engine is running or notRotating or startingToCrank
  4976. ; Check if we should reset vapor lock flag: 3 minutes after
  4977. ; engine was started or if o2Fbk < $4d (meaning we are running rich)
  4978. ;--------------------------------------------------------------------
  4979. L1302 ldaa T0p5_crank1 ;
  4980. adda #$5a ;
  4981. bcc L1303 ; branch to reset flag if engine stopped "startingToCrank" more than 180s ago (engine has been running for 3 minutes)
  4982. ldaa o2Fbk ; a = o2Fbk
  4983. cmpa #$4d ;
  4984. bhi L1304 ; Dont reset if o2Fbk > $4d (running lean???)
  4985. L1303 andm fpsBcsFlags, #$f7 ; Reset vappor lock flag
  4986.  
  4987. ;---------------------------------------------
  4988. ; Check whether we have vapor lock conditions
  4989. ;---------------------------------------------
  4990. L1304 brset fpsBcsFlags, #$08, L1305 ; Branch if vapor lock flag is set
  4991.  
  4992. ;---------------------------------------------------------------------
  4993. ; Vapor lock flag is not set, at this point b = old fpsBcsFlags
  4994. ; Check if solenoid deactivation is just happening now and
  4995. ; set flag to indicate so
  4996. ;---------------------------------------------------------------------
  4997. bitb #$08 ;
  4998. beq L1307 ; Branch to reset solenoid if bit was also 0 on previous iteration
  4999. orm fpsBcsFlags, #$04 ; Set flag indicating solenoid was just deactivated
  5000. bra L1307 ; Branch to deactivate it
  5001.  
  5002. ;----------------------------------------------
  5003. ; Vapor lock flag is set, check additional
  5004. ; conditions before activating solenoid
  5005. ;----------------------------------------------
  5006. L1305 ldaa T40_crank ;
  5007. adda #$50 ;
  5008. bcs L1306 ; branch to activate solenoid if engine stopped "startingToCrank" less than 2s ago
  5009. brset closedLpFlags, #$01, L1307 ; Branch to deactivate solenoid if the ECU has determined that we should be using closed loop mode (or getting close to it)
  5010.  
  5011. ;-------------------------------------
  5012. ; Activate the fuel pressure solenoid
  5013. ;-------------------------------------
  5014. L1306 andm port5, #$ef ; Activate the fuel pressure solenoid
  5015. bra L1309
  5016.  
  5017. ;----------------------------------------
  5018. ; Deactivate the fuel pressure solenoid
  5019. ;----------------------------------------
  5020. L1307 orm port5, #$10 ; Deactivate the fuel pressure solenoid
  5021. L1309 rts
  5022.  
  5023.  
  5024.  
  5025. ;******************************************************************
  5026. ;
  5027. ;
  5028. ; Second subroutine
  5029. ;
  5030. ;
  5031. ;******************************************************************
  5032. ;--------------------------------------------------
  5033. ; Build port3Snap1 from port3Snap0 using a
  5034. ;
  5035. ; Set port3Snap1.2 if vssCnt1!=0, reset otherwise
  5036. ;--------------------------------------------------
  5037. subroutine2 ldaa port3Snap0 ; start with a = port3Snap1 = port3Snap0
  5038. anda #$fb ; reset 00000100
  5039. ldab vssCnt1 ; b = vssCnt1
  5040. beq L1311 ;
  5041. oraa #$04 ; Set flag
  5042.  
  5043. ;---------------------------------------------------------------
  5044. ; Reset iscFlags0.6 if key is in start and T40_noPower not expired??????
  5045. ; (meaning engine is cranking and battery not KO?
  5046. ;---------------------------------------------------------------
  5047. L1311 bita #$40 ;
  5048. bne L1312 ; Bail if key is not is start
  5049. tst T40_noPower ; Key in start
  5050. beq L1312 ; Bail if timer expired
  5051. andm iscFlags0, #$bf ; Key in start and timer not expired, reset max calibration flag 0100 0000
  5052.  
  5053. ;----------------------------------------------------------
  5054. ; Re-init T40_noPower at 5 (0.125s) if ECU still has power
  5055. ;----------------------------------------------------------
  5056. L1312 bita #$02 ;
  5057. bne L1313 ; Branch if IG1 at 0V (No more power, ECU about to turn off?)
  5058. ldab #$05 ; ECU not about to turn off, restart timer
  5059. stab T40_noPower ;
  5060.  
  5061. ;-------------------------------------------
  5062. ; Move old port3Snap1 to oldP3Snap1 and
  5063. ; update port3Snap1 with new value
  5064. ;-------------------------------------------
  5065. L1313 ldab port3Snap1 ; b = old port3Snap1
  5066. staa port3Snap1 ; port3Snap1 = new port3Snap1
  5067. stab oldP3Snap1 ; oldP3Snap1 = old port3Snap1
  5068.  
  5069. ;-------------------
  5070. ; Update iscStepMax
  5071. ;-------------------
  5072. ldaa #$78 ; I believe this is the max iscStepCurr value (120 decimal)
  5073. staa iscStepMax ; iscStepMax = max possible value?
  5074. nop ;
  5075. nop ;
  5076. nop ;
  5077.  
  5078. ;---------------------------------------------------------------
  5079. ; Decrement T40s_iscStable at 40Hz if iscStepCurr = iscStepTarg
  5080. ;---------------------------------------------------------------
  5081. ldaa iscStepTarg ;
  5082. cmpa iscStepCurr ;
  5083. bne L1314 ; Branch if iscStepCurr != iscStepTarg
  5084. ldx #T40s_iscStable ;
  5085. jsr decX40Hz ; Decrement T40s_iscStable at 40Hz
  5086.  
  5087. ;-----------------------------------------------------
  5088. ; If engine is notRotating, re-init some ISC variables
  5089. ;-----------------------------------------------------
  5090. L1314 brclr state1, #$10, L1315 ; Bail if notRotating clear
  5091. jsr iscYnInit ; Init isc variables
  5092. clr iscStStall ;
  5093. #ifdef E932
  5094. clr iscStBaseAcAdj ;
  5095. #endif
  5096. andm iscFlags1, #$5f ; reset flags used when engine rotating or running, 1010 0000
  5097. orm iscFlags1, #$01 ; set flag (flag is only 0 when key in start and iscStTargSpec = iscStepCurr)
  5098.  
  5099. ;-----------------------------------------------------
  5100. ; Update isc stable timer if power
  5101. ; steering flag changed since last time
  5102. ;-----------------------------------------------------
  5103. L1315 ldaa port3Snap1 ;
  5104. eora oldP3Snap1 ;
  5105. bita #$08 ;
  5106. beq L1316 ; Branch if port3Snap1.3 did not change value since last time
  5107. ldab #$50 ;
  5108. jsr updIscStableTimer ;
  5109.  
  5110. ;-----------------------------------------------------
  5111. ; Update isc stable timer if A/C state
  5112. ; changed since last time
  5113. ;-----------------------------------------------------
  5114. L1316 bita #$10 ;
  5115. beq L1317 ; Branch if port3Snap1.4 did not change value since last time
  5116. ldab #$50 ;
  5117. jsr updIscStableTimer ;
  5118. L1317 .equ $ ;
  5119.  
  5120. ;-----------------------------------------------------
  5121. ; Update isc stable timer if park/neutral
  5122. ; changed since last time (E932)
  5123. ;-----------------------------------------------------
  5124. #ifdef E932
  5125. bita #$20 ;
  5126. beq L1319 ; Branch if port3Snap1.5 did not change value since last time
  5127. ldab #$50 ;
  5128. brclr port3Snap1, #$20, L1318 ; Makes no difference, $50 used anyway...
  5129. ldab #$50 ;
  5130. L1318 jsr updIscStableTimer ;
  5131. #endif
  5132.  
  5133. ;-----------------------------------------------------
  5134. ; Update isc stable timer if tpsDiffMax2 > $04
  5135. ; i.e. gas pedal is moving...
  5136. ;-----------------------------------------------------
  5137. L1319 ldaa tpsDiffMax2 ;
  5138. cmpa #$04 ;
  5139. bcs L1320 ;
  5140. ldab #$28 ;
  5141. jsr updIscStableTimer ;
  5142.  
  5143. ;----------------------------------------------------------------
  5144. ; Update isc stable timer if idle switch is off
  5145. ; Timer value is from table t_iscStableIdleSw
  5146. ; Timer will only start counting when idle switch is back on...
  5147. ;----------------------------------------------------------------
  5148. L1320 ldaa port3Snap1 ;
  5149. bmi L1323 ; Bail if idle switch is ON
  5150. ldd rpm4 ;
  5151. lsrd ; d = rpm4/2
  5152. subb idleSpdTarg ;
  5153. sbca #$00 ; d = rpm4/2 - idleSpdTarg
  5154. bcc L1321 ; Branch if no overflow
  5155. clra ;
  5156. clrb ; Use min of d=0
  5157. L1321 lsrd ;
  5158. lsrd ; d = (rpm4/2 - idleSpdTarg)/4
  5159. cmpd #$00a0 ;
  5160. bcs L1322 ; Branch if (rpm4/2 - idleSpdTarg)/4 < $a0 (5000rpm)
  5161. ldab #$a0 ; Use max of $a0 (5000rpm)
  5162. L1322 tba ; a = (rpm4/2 - idleSpdTarg)/4
  5163. ldx #t_iscStableIdleSw ;
  5164. jsr interp16b ; b = t_iscStableIdleSw((rpm4/2 - idleSpdTarg)/4)
  5165. jsr updIscStableTimer ;
  5166.  
  5167. ;---------------------------------------------------
  5168. ; Update isc stable timer if engine is not runnning
  5169. ; or if min or max isc calibration is ongoing or if
  5170. ; we have ignition problems
  5171. ;---------------------------------------------------
  5172. L1323 brset state1, #$11, L1324 ; Branch to update if notRotating or startingToCrank
  5173. brset iscFlags0, #$a0, L1324 ; Branch to update if min or max calibration requested flag is set
  5174. brclr coilChkFlags, #$80, L1325 ; Bail if no problem found on ignition signal
  5175. L1324 ldab #$78 ;
  5176. jsr updIscStableTimer ;
  5177.  
  5178. ;-----------------------------------------------------------------
  5179. ; Update idleSpdInit = t_idleSpd(ect) or t_idleSpdDr(ect) (E932)
  5180. ;-----------------------------------------------------------------
  5181. L1325 .equ $
  5182. #ifdef E931
  5183. ldx #t_idleSpd ;
  5184. jsr interpEct ; b = t_idleSpd(ect)
  5185. #else
  5186. ldx #t_idleSpdDr ;
  5187. brclr port3Snap1, #$20, L1326 ;
  5188. ldx #t_idleSpd ;
  5189. L1326 jsr interpEct ;
  5190. #endif
  5191. stab idleSpdInit ; idleSpdInit = t_idleSpd(ect) or t_idleSpdDr(ect)
  5192.  
  5193. ;-----------------------------------------------------------------
  5194. ; Update idleSpdMin with 0 if A/C switch is on
  5195. ; or if AT is in drive or if T0p5_crCold timer expired
  5196. ;-----------------------------------------------------------------
  5197. clrb ; preload default value of 0
  5198. ldaa port3Snap1 ;
  5199. anda #$30 ;
  5200. cmpa #$30 ;
  5201. bne L1328 ; Branch to use default of 0 if A/C switch is on or if AT is in drive
  5202. ldaa T0p5_crCold ;
  5203. beq L1328 ; Branch to use default of 0 if T0p5_crCold timer expired
  5204.  
  5205. ;-----------------------------------------------
  5206. ; A/C switch is off and AT is in not in drive
  5207. ; and T0p5_crCold timer not expired
  5208. ;
  5209. ; Compute idleSpdMin
  5210. ;-----------------------------------------------
  5211. ldaa #$3c ; a = $3c
  5212. staa temp1 ; temp1 = $3c
  5213. ldaa #$13 ; a = $13
  5214. brset iscFlags1, #$20, L1327 ; Branch if engine startingToCrank and temperature(iat) < 75degC
  5215. #ifdef E931
  5216. ldaa #$20 ; Use higher value
  5217. #else
  5218. ldaa #$13 ; Use same value
  5219. #endif
  5220. L1327 ldab T0p5_crCold ; b = T0p5_crCold
  5221. mul ; d = ($13 or $20) * T0p5_crCold
  5222. div temp1 ; b = ($13 or $20) * T0p5_crCold / $3c
  5223. addb t_idleSpd ; b = ($13 or $20) * T0p5_crCold / $3c + t_idleSpd(0)
  5224. L1328 stab idleSpdMin ; idleSpdMin = 0 or (t_idleSpd(0) + (0.32 0r 0.53) * T0p5_crCold)
  5225.  
  5226. ;----------------------------------------------------------------------------------
  5227. ; Update idleSpdTarg from idleSpdInit and idleSpdMin and A/C park/neutral conditions
  5228. ;----------------------------------------------------------------------------------
  5229. ldaa idleSpdInit ; a = idleSpdInit
  5230. brset port3Snap1, #$10, L1331 ; Branch if A/C switch is off?
  5231. #ifdef E932
  5232. ldab #$53 ; 648rpm
  5233. brclr port3Snap1, #$20, L1330 ; Branch if park/neutral???
  5234. #endif
  5235. ldab #$6d ; 852rpm
  5236. L1330 cba ;
  5237. bcc L1331 ; Branch if idleSpdInit >= $6d
  5238. tba ; Use min of a = $6d
  5239.  
  5240. L1331 cmpa idleSpdMin ;
  5241. bcc L1332 ; Branch if idleSpdInit >= idleSpdMin
  5242. ldaa idleSpdMin ; Use min of idleSpdMin
  5243. L1332 staa idleSpdTarg ; idleSpdTarg = ...
  5244.  
  5245.  
  5246. #ifdef E931
  5247. ;-------------------------------------------
  5248. ; Update iscStBase for E931
  5249. ; iscStBase = t_iscStEct0(ect)
  5250. ;-------------------------------------------
  5251. ldx #t_iscStEct0 ;
  5252. jsr interpEct ;
  5253. stab iscStBase ; iscStBase = t_iscStEct0(ect)
  5254. #else
  5255. ;------------------------------------------------------------------
  5256. ; Update iscStBase for E932
  5257. ; Choose a different table if transmission is engaged (e.g. drive)
  5258. ;
  5259. ; iscStBase = t_iscStEct1(ect) or t_iscStEct0(ect)
  5260. ;------------------------------------------------------------------
  5261. ldx #t_iscStEct1 ;
  5262. brclr port3Snap1, #$20, L1334 ; Branch if park/neutral
  5263. ldx #t_iscStEct0 ;
  5264. L1334 jsr interpEct ;
  5265. stab iscStBase ; iscStBase = t_iscStEct0(ect) or t_iscStEct1(ect)
  5266.  
  5267. ;-------------------------------------------------------------------
  5268. ; For E932, decrement iscStBaseAcAdj by 1 (min of 0) at ~2.2Hz
  5269. ;-------------------------------------------------------------------
  5270. ldaa T40_21 ; a = T40_21
  5271. bne L1336 ; Branch if timer not expired
  5272. ldaa #$12 ; Timer expired, re-init to 0.45sec
  5273. staa T40_21 ; T40_21 = 0.45sec
  5274. ldaa iscStBaseAcAdj ; a = iscStBaseAcAdj
  5275. suba #$01 ; a = iscStBaseAcAdj - 1
  5276. bcc L1335 ; Branch if no underflow
  5277. clra ; Underflow, use min of 0
  5278. L1335 staa iscStBaseAcAdj ; update iscStBaseAcAdj
  5279. #endif
  5280.  
  5281. ;---------------------------------------------------
  5282. ; Compute iscStBaseAc, basic iscStep corrected for
  5283. ; additional A/C and transmission load
  5284. ;---------------------------------------------------
  5285. L1336 clra ; preload default value of offset = 0
  5286. brset port3Snap1, #$10, L1342 ; Bail to use offset of 0 if A/C switch off
  5287. ldaa #$1c ; A/C is on, offset = a = $1c
  5288. ldab #$37 ; minValue = b = $37
  5289. #ifdef E932
  5290. brset port3Snap1, #$20, L1337 ; Branch if park/neutral
  5291. ldaa #$0a ; AT in drive, offset = $0a
  5292. ldab #$25 ; minValue = $25
  5293. #endif
  5294. L1337 adda iscStBase ; a = t_iscStEct0(ect) + offset
  5295. bcc L1338 ; Branch if no overflow
  5296. ldaa #$ff ; Use max of $ff
  5297. L1338 cba ;
  5298. bcc L1339 ; Branch if t_iscStEct0(ect) + offset >= minValue
  5299. tba ; Use minValue
  5300. L1339 .equ $
  5301. #ifdef E932
  5302. brclr port3Snap1, #$20, L1341 ; Branch if AT not in park/neutral
  5303. staa iscStBaseAcAdj ; AT in park, iscStBaseAcAdj = t_iscStEct0(ect) + offset
  5304. bra L1342 ;
  5305. L1341 cmpa iscStBaseAcAdj ; AT in drive, update iscStBaseAc with minimum of iscStBaseAcAdj
  5306. bcc L1342 ;
  5307. ldaa iscStBaseAcAdj ;
  5308. #endif
  5309. L1342 staa iscStBaseAc ; For E931, iscStBaseAc = min(t_iscStEct0(ect) + offset, minValue)
  5310.  
  5311.  
  5312. ;------------------------------------------------------------------
  5313. ; Init iscStStartUsed and iscStStartMaster as long as we are
  5314. ; startingToCrank. These values are used at engine startup
  5315. ;------------------------------------------------------------------
  5316. brclr state1, #$01, L1343 ; Bail if startingToCrank clear
  5317. ldx #L2023 ; Engine is startingToCrank
  5318. jsr interpEct ; b = L2023(ect)
  5319. stab iscStStartUsed ; iscStStartUsed = L2023(ect)
  5320. stab iscStStartMaster ; iscStStartMaster = L2023(ect)
  5321.  
  5322. ;----------------------------------------------------------------
  5323. ; Section to update iscStStartUsed, iscStStartMaster
  5324. ; and eventually iscYn after the engine is started
  5325. ;
  5326. ; learning variable iscYn is only updated after a certain delay
  5327. ; has passed since the engine was started. At that point we have
  5328. ; a good idea of how far we are from the ideal isc step we
  5329. ; should be using...
  5330. ;----------------------------------------------------------------
  5331. ;------------------------------------------
  5332. ; First check if basic conditions are met
  5333. ;------------------------------------------
  5334. L1343 brset state1, #$11, L1349 ; bail if notRotating or startingToCrank
  5335. ldaa iscStStartMaster ; a = iscStStartMaster
  5336. beq L1349 ; Bail if iscStStartMaster = 0 (we already updated iscYn once)
  5337. ldaa T40_iscStart ;
  5338. bne L1347 ; Branch if T40_iscStart no yet expired
  5339.  
  5340. ;---------------------------------------------------------------
  5341. ; Engine is running, iscStStartMaster!=0 and T40_iscStart is expired
  5342. ;---------------------------------------------------------------
  5343. ;--------------------------------------------------------------------------
  5344. ; Decrement iscStStartMaster by 1 (min of 0) at around 6Hz (3Hz if cold)
  5345. ;
  5346. ; T40_iscStart is initialized with values from L2024(ect) on
  5347. ; every timer expiry, if L2024(ect)=7 then freqency
  5348. ; will be around 6Hz... In colder temperature, iscStStartMaster will
  5349. ; be decremented at a slower rate, e.g 3Hz
  5350. ;--------------------------------------------------------------------------
  5351. ldx #L2024 ; x points to L2024
  5352. jsr interpEct ; b = L2024(ect)
  5353. stab T40_iscStart ; T40_iscStart = L2024(ect)
  5354. dec iscStStartMaster ; iscStStartMaster = iscStStartMaster - 1
  5355. bne L1347 ; Branch if iscStStartMaster!=0
  5356.  
  5357. ;------------------------------------------------------------------------
  5358. ; iscStStartMaster reached 0, update iscYn = old iscYn + iscStStartUsed + L2023(ect)
  5359. ; and reset iscStStartUsed since we are now finished updating iscYn
  5360. ; i.e. both iscStStartMaster and iscStStartUsed are now 0
  5361. ;
  5362. ; Basically, update the isc learning variables with how much isc offset
  5363. ; was required to get the isc step stable upon engine startup. If isc
  5364. ; was not stable then iscStStartUsed=0 and we didn't learn anything...
  5365. ;------------------------------------------------------------------------
  5366. jsr iscPointers ;
  5367. ldaa iscStStartUsed ; a = iscStStartUsed
  5368. adda $00,y ; a = iscStStartUsed + iscYn, y = y+1
  5369. decy ; y = y - 1
  5370. bcc L1346 ; Branch if no overflow
  5371. ldaa #$ff ; Use max of $ff
  5372. L1346 staa $00,y ; iscYn = old iscYn + iscStStartUsed + L2023(ect)
  5373. clr iscStStartUsed ; iscStStartUsed = 0 since we are now finished updating iscYn
  5374.  
  5375. ;-------------------------------------------------
  5376. ; Check if we should re-init isc Yn variables???
  5377. ;-------------------------------------------------
  5378. L1347 ldaa T40s_iscStable ;
  5379. bne L1348 ; Branch to re-init if T40s_iscStable not expired, i.e. isc is not yet stable
  5380. brset iscLrnFlags, #$10, L1350 ; Branch if conditions are good to update isc learning variables
  5381.  
  5382. ;-------------------------------------------------
  5383. ; At this point, isc is not yet stable or
  5384. ; conditions are not good to update isc variables
  5385. ;
  5386. ; Re-init isc Yn variables
  5387. ;-------------------------------------------------
  5388. L1348 jsr iscYnInit ;
  5389.  
  5390. ;----------------------------------------------------------------------------
  5391. ; At this point isc not yet stable or engine notRotating or startingToCrank
  5392. ; iscStStartUsed = iscStStartMaster
  5393. ;
  5394. ; Basically synch the isc step currently in use with the master value
  5395. ;----------------------------------------------------------------------------
  5396. L1349 ldaa iscStStartMaster ; a = iscStStartMaster
  5397. staa iscStStartUsed ; iscStStartUsed = iscStStartMaster
  5398.  
  5399. ;-----------------------------------------------------------------------
  5400. ; Decrement iscStStall by 3 (min of 0) at 20Hz (T40_stall looping at 2?)???
  5401. ;-----------------------------------------------------------------------
  5402. L1350 ldaa iscStStall ; a = iscStStall
  5403. beq L1355 ; Bail if iscStStall already at 0
  5404. ldab T40_stall ;
  5405. bne L1352 ; Branch if T40_stall not expired
  5406. suba #$03 ; a = iscStStall-3
  5407. bcc L1351 ; Branch if no underflow
  5408. clra ; use min of 0
  5409. L1351 staa iscStStall ; iscStStall = max(iscStStall-3, 0)
  5410. L1352 ldab #$02 ; b = $02
  5411. #ifdef E932
  5412. brset port3Snap1, #$20, L1353 ; Branch if park/neutral
  5413. ldab #$02 ; Use same value anyway...
  5414. #endif
  5415. L1353 ldaa T40_stall ; a = T40_stall
  5416. beq L1354 ; Branch if T40_stall expired
  5417. cba ;
  5418. bcc L1354 ; Branch to use 2 if T40_stall >= 2
  5419. tab ; Use b = T40_stall when T40_stall < 2
  5420. L1354 stab T40_stall ; T40_stall = min(T40_stall, 2)
  5421.  
  5422. ;--------------------------------------------
  5423. ; Re-init T40_revving to 0.5sec if
  5424. ; tpsRaw < 86% or airVol >= $3a ???
  5425. ;
  5426. ; T40_revving will start counting when tpsRaw>86%
  5427. ; and airVol < $3a????
  5428. ;--------------------------------------------
  5429. L1355 ldaa tpsRaw ; a = tpsRaw
  5430. cmpa #$dc ;
  5431. bcs L1356 ; Branch to re-init if tpsRaw < 86%
  5432. ldaa airVol ; a = airVol
  5433. cmpa #$3a ;
  5434. bcs L1357 ; Branch if airVol < $3a
  5435. L1356 ldaa #$14 ; 0.5sec
  5436. staa T40_revving ; Re-init T40_revving to 0.5sec
  5437.  
  5438. ;-------------------------------------------------
  5439. ; Set iscFlags1.7 flag if T40_revving is expired
  5440. ;
  5441. ; Basically, this flag is set when tps has been
  5442. ; high and airVol low for more than 0.5sec
  5443. ;-------------------------------------------------
  5444. L1357 andm iscFlags1, #$7f ; Assume flag is 0
  5445. ldaa T40_revving ;
  5446. bne L1358 ; Branch if T40_revving not expired
  5447. orm iscFlags1, #$80 ; T40_revving expired, set flag
  5448.  
  5449. ;---------------------------------------------------------
  5450. ; Section to update iscStStall as long as idle switch
  5451. ; is off and rpm8>=500 and iscFlags1.7 = 0 (set to 1 when
  5452. ; tps has been high and airVol low for more than 0.5sec)
  5453. ;
  5454. ; iscStStall will therefore be "stuck" to the value
  5455. ; calculated when all these conditions were met the last
  5456. ; time. Basically says where we are coming from when
  5457. ; the throttle plate closes (likeliness of stalling the
  5458. ; engine...)??
  5459. ;---------------------------------------------------------
  5460. ;------------------------------
  5461. ; First check those conditions
  5462. ;------------------------------
  5463. L1358 ldaa port3Snap1 ;
  5464. bmi L1362 ; Bail if idle position switch is on
  5465. ldaa iscFlags1 ;
  5466. bmi L1362 ; Bail if tps has been high and airVol low for more than 0.5sec
  5467. ldaa rpm8 ;
  5468. cmpa #$40 ; 500rpm
  5469. bcs L1362 ; Bail if rpm8 < 500
  5470.  
  5471. ;------------------------------------------------
  5472. ; Set flag, not directly related to calculation
  5473. ;------------------------------------------------
  5474. orm iscLrnFlags, #$20 ;
  5475.  
  5476. ;---------------------------------------------------------------
  5477. ; Compute conditionned tps and store in temp2 for
  5478. ; table interpolation below
  5479. ; condTps (with range of $00 to $a0) =
  5480. ; 2* max(min(tpsRaw,$ba)-$1a,0) if tpsRaw <= 23%
  5481. ; max(min(tpsRaw,$ba)-$1a,0) + $20 if tpsRaw > 23%
  5482. ;---------------------------------------------------------------
  5483. ldab tpsRaw ;
  5484. ldx #$ba1a ;
  5485. jsr clipOffset ; b = max(min(tpsRaw,$ba)-$1a,0)-> returns b = $00 to $a0 (tpsRaw 10% to 73%)
  5486. cmpb #$20 ;
  5487. bhi L1359 ; Branch if max(min(tpsRaw,$ba)-$1a,0) > $20 (tpsRaw>23%)
  5488. aslb ; b = 2* max(min(tpsRaw,$ba)-$1a,0)
  5489. bra L1360 ;
  5490. L1359 addb #$20 ; b = max(min(tpsRaw,$ba)-$1a,0) + $20
  5491. L1360 stab temp2 ; temp2 = condTps
  5492.  
  5493. ;--------------------------------------------------------------
  5494. ; Update iscStStall = max(old iscStStall, t_iscStStall(condTps))
  5495. ;--------------------------------------------------------------
  5496. ldx #t_iscStStall ; x points to t_iscStStall
  5497. #ifdef E932
  5498. brset port3Snap1, #$20, L1361 ; Branch if park/neutral
  5499. ldx #L2030 ; x points to L2030 for E932
  5500. #endif
  5501. L1361 ldaa temp2 ; a = condTps
  5502. jsr interp32 ; b = t_iscStStall(condTps)
  5503. cmpb iscStStall ;
  5504. bcs L1362 ; Branch if t_iscStStall(condTps) < iscStStall
  5505. stab iscStStall ; iscStStall = max(old iscStStall, t_iscStStall(condTps))
  5506.  
  5507. ;--------------------------------------------------------
  5508. ; If idle switch if off,
  5509. ; subtract (iscStepTarg - iscStepCurr) from iscStStall
  5510. ;
  5511. ; Basically reduce iscStStall if the current isc step is
  5512. ; lower than the target. Reduce it by the same amount...
  5513. ;--------------------------------------------------------
  5514. L1362 ldaa port3Snap1 ; a = port3Snap1
  5515. bpl L1365 ; Bail if idle switch is on
  5516. eora oldP3Snap1 ;
  5517. bpl L1365 ; Bail if it changed in that split second?????? (Am I missing something??? maybe they changed their mind...)
  5518.  
  5519. ldab iscStepTarg ; b = iscStepTarg
  5520. subb iscStepCurr ; b = iscStepTarg - iscStepCurr
  5521. bcc L1363 ; Branch if result positive
  5522. clrb ; Use min of 0
  5523. L1363 ldaa iscStStall ; a = iscStStall
  5524. sba ; a = iscStStall - (iscStepTarg - iscStepCurr)
  5525. bcc L1364 ; Branch if no underflow
  5526. clra ; Use min of 0
  5527. L1364 staa iscStStall ; iscStStall = iscStStall - (iscStepTarg - iscStepCurr)
  5528.  
  5529. ;----------------------------------
  5530. ; Update iscStBarOff
  5531. ;----------------------------------
  5532. L1365 ldaa baroCond ; a = baroCond
  5533. ldx #t_iscStBaro ;
  5534. jsr interp32 ; b = t_iscStBaro(baroCond)
  5535. stab iscStBarOff ; iscStBarOff = t_iscStBaro(baroCond)
  5536.  
  5537. ;--------------------------------------------
  5538. ; Reset T0p5_crCold to 0 if notRotating
  5539. ; or if T0p5_crCold >= $3c (not possible???)
  5540. ;--------------------------------------------
  5541. brset state1, #$10, L1367 ; Branch to reset T0p5_crCold if notRotating
  5542. ldaa #$3c ;
  5543. cmpa T0p5_crCold ;
  5544. bcc L1368 ; Branch if T0p5_crCold <= $3c
  5545. L1367 clr T0p5_crCold ; reset T0p5_crCold to 0
  5546.  
  5547. ;---------------------------------------
  5548. ; Update T0p5_crCold to $00 or $3c
  5549. ; and iscFlags1.1 flag when startingToCrank
  5550. ;---------------------------------------
  5551. L1368 brclr state1, #$01, L1370 ; Bail if startingToCrank clear
  5552. clrb ; preload b=0
  5553. ldaa ectFiltered ;
  5554. cmpa #$1b ; 88degC
  5555. bcc L1369 ; Branch if temperature(ectFiltered) <= 88degC
  5556. ldab #$3c ; b= $3c
  5557. L1369 stab T0p5_crCold ; T0p5_crCold = 0 or $3c
  5558. andm iscFlags1, #$df ; Assume we reset bit
  5559. ldaa iatChecked ;
  5560. cmpa #$29 ; 75degC
  5561. bcs L1370 ; Branch if temperature(iat) > 75degC
  5562. orm iscFlags1, #$20 ; Set flag
  5563.  
  5564. ;-------------------------------------------------------------
  5565. ; Update iscStBaseCSt if T0p5_crCold not expired, else use iscStBaseCSt = 0
  5566. ;
  5567. ; iscStBaseCSt = (0 or $1e or $0f) * T0p5_crCold / $3c + t_iscStEct0
  5568. ;
  5569. ; iscStBaseCSt is basically the iscStep when a cold engine
  5570. ; is being started, starts with a high value and then is
  5571. ; decreased (through T0p5_crCold) towards normal isc step
  5572. ; over a period of 120sec
  5573. ;-------------------------------------------------------------
  5574. L1370 clrb ; preload b = 0
  5575. ldaa T0p5_crCold ;
  5576. beq L1372 ; Bail to store 0 if T0p5_crCold expired
  5577. ldaa #$3c ;
  5578. staa temp1 ; temp1 = $3c
  5579. #ifdef E931
  5580. ldaa #$1e ; assume low iat, high value...a = $1e
  5581. #else
  5582. ldaa #$19 ;
  5583. #endif
  5584. brclr iscFlags1, #$20, L1371 ; Branch if not (startingToCrank and temperature(iat) < 75degC)
  5585. ldaa #$0f ; high iat, use lower value... a = $0f
  5586. L1371 ldab T0p5_crCold ; b = T0p5_crCold
  5587. mul ; d = $1e * T0p5_crCold
  5588. div temp1 ; d = $1e * T0p5_crCold / $3c
  5589. addb t_iscStEct0 ; b = $1e * T0p5_crCold / $3c + t_iscStEct0
  5590. L1372 stab iscStBaseCSt ; iscStBaseCSt = 0 or $1e * T0p5_crCold / $3c + t_iscStEct0
  5591.  
  5592. ;--------------------------------------------------
  5593. ; Check if iscStepCurr and iscStepCom are in synch?
  5594. ;--------------------------------------------------
  5595. sei ; We don't want ISC values to change during check
  5596. ldaa iscStepCurr ;
  5597. coma ;
  5598. anda #$7f ;
  5599. cmpa iscStepCom ;
  5600. cli ;
  5601. bne L1373 ; Branch if iscStepCurr and iscStepCom are not in synch
  5602.  
  5603. ;-------------------------------------------
  5604. ; iscStepCurr and iscStepCom are in synch
  5605. ; Check ISC variables against min/max
  5606. ;-------------------------------------------
  5607. ldx #$b000 ; x = $b000
  5608. cpx isc0 ;
  5609. bcs L1373 ; Branch to re-init if isc0 > $b000
  5610. cpx isc1 ;
  5611. bcs L1373 ; Branch to re-init if isc1 > $b000
  5612. ldx #$6c00 ; x = $6c00
  5613. cpx isc0 ;
  5614. bhi L1373 ; Branch to re-init if isc0 < $6c00
  5615. cpx isc1 ;
  5616. bhi L1373 ; Branch to re-init if isc1 < $6c00
  5617. brset iscFlags0, #$20, L1374 ; Branch if the ISC needs min calibration
  5618. bra L1379 ; Branch to continue processing normal flow???
  5619.  
  5620. ;-----------------------------------------------
  5621. ; iscStepCurr and iscStepCom are not in synch
  5622. ; re-initialize ISC variables
  5623. ; set isc flag indicating we need min calibration
  5624. ; reset all other isc flags
  5625. ;-----------------------------------------------
  5626. L1373 ldd #$8c00 ;
  5627. std isc0 ;
  5628. std isc1 ;
  5629. jsr iscYnInit ;
  5630. ldaa #$20 ; Set flag indicating we need to calibrate ISC, reset all other flags
  5631. staa iscFlags0 ;
  5632.  
  5633. ;------------------------------------------------------------------------
  5634. ; Section for ISC min calibration, code is triggered by iscFlags0.5 being set
  5635. ;
  5636. ; Min calibration proceed as follows.
  5637. ;
  5638. ; 1) iscFlags0.5 is set to indicate calibration is required,
  5639. ; iscStepCurr init to 135, iscStepTarg init to 0
  5640. ; 2) iscFlags0.0 is set to indicate calibration is started,
  5641. ; waiting for iscStepCurr to reach iscStepTarg of 0
  5642. ; 3) iscFlags0.1 is set to indicate calibration is finished,
  5643. ; iscStepCurr reached 0, we are therefore now certain that
  5644. ; the isc pintle is physically at position 0.iscStepTarg
  5645. ; is now init to 6, waiting for iscStepCurr to reach iscStepTarg
  5646. ; 4) iscStepCurr reached iscStepTarg = 6, iscFlags0.0.1.5
  5647. ; are all reset
  5648. ;
  5649. ;------------------------------------------------------------------------
  5650. L1374 brset iscFlags0, #$02, L1376 ; Branch if calibration is finished, we are waiting for ISC to go back to iscStepCurr=6
  5651. brset iscFlags0, #$01, L1375 ; Branch if calibration is started, we are waiting for ISC to reach iscStepCurr=0
  5652. ldaa #$87 ; calibration not started, use iscStepCurr = a = $87 (135, above max, maybe during calibration?)
  5653. sei ;
  5654. jsr iscStepComp ; iscStepCurr = $87, iscStepCom = (~$87 & 7F)
  5655. cli ;
  5656. orm iscFlags0, #$01 ; Set flag indicating we just calculated iscStepCom?
  5657. L1375 clrb ; use iscStepTarg = b = 0
  5658. ldaa iscStepCurr ; a = iscStepCurr
  5659. bne L1377 ; Branch if iscStepCurr != 0
  5660. orm iscFlags0, #$02 ; iscStepCurr=0, set flag
  5661. L1376 ldab #$06 ; b = $06
  5662. cmpb iscStepCurr ;
  5663. beq L1378 ; Branch if iscStepCurr=$06
  5664. L1377 stab iscStepTarg ; iscStepTarg = $00 or $06
  5665. jmp L1389 ; Bail
  5666. L1378 clra ; a = 0
  5667. sei ;
  5668. jsr iscStepComp ; iscStepCurr = $0, iscStepCom = (~$0 & 7F)
  5669. cli ;
  5670. andm iscFlags0, #$dc ; Calibration is over, reset all flags 0010 0011
  5671.  
  5672. ;--------------------------------------------------------
  5673. ; Normal flow continues,
  5674. ; Check if max calibration need to be performed
  5675. ;--------------------------------------------------------
  5676. L1379 brset iscFlags0, #$80, L1380 ; Branch if max calibration is required?
  5677. tst T40_noPower ;
  5678. bne L1382 ; Branch to normal flow if we are not about to loose power
  5679. brset iscFlags0, #$40, L1382 ; Branch to normal flow if max calibration already performed
  5680.  
  5681. ;-------------------------------------------------------------
  5682. ; At this point max calibration flag is set or we are about
  5683. ; to loose power and max calibration was not performed
  5684. ;
  5685. ; Set iscStepTarg to 135 if iscStepCurr not already at 135
  5686. ;-------------------------------------------------------------
  5687. L1380 orm iscFlags0, #$80 ; Set max calibration flag is case it was not set
  5688. ldaa #$87 ; a = 135
  5689. cmpa iscStepCurr ;
  5690. beq L1381 ; Branch if iscStepCurr already at 135
  5691. staa iscStepTarg ; Set target to iscStepTarg = 135
  5692. jmp L1389 ;
  5693.  
  5694. ;-------------------------------------------------------------
  5695. ; At this point iscStepCurr is 135, we are therefore sure the
  5696. ; isc pintle is physically at its maximum value of 120,
  5697. ; set iscStepCurr=120 and set/reset flags
  5698. ;-------------------------------------------------------------
  5699. L1381 ldaa #$78 ; a = $78 (120, max usable value)
  5700. sei ;
  5701. jsr iscStepComp ; iscStepCurr = $78, iscStepCom = (~$78 & 7F)
  5702. cli ;
  5703. orm iscFlags0, #$40 ; set flag 0100 0000, calibration done?
  5704. andm iscFlags0, #$7f ; reset flag indicating we need max calibration 1000 0000
  5705.  
  5706. ;-----------------------------------------------------
  5707. ; if the ECU is about to loose power then
  5708. ;
  5709. ; set/reset flags
  5710. ; use a fixed ISC step of $5a
  5711. ; don't re-init updIscStableTimer since we loose power...
  5712. ;-----------------------------------------------------
  5713. L1382 andm iscFlags0, #$ef ; Assume we reset 0001 0000, updated below
  5714. ldaa T40_noPower ;
  5715. bne L1383 ; Branch if timer not expired
  5716. andm iscFlags0, #$40 ; reset max calibration flag? 0100 0000
  5717. orm iscFlags0, #$10 ; Set 0001 0000
  5718. ldab #$5a ; b = $5a
  5719. bra L1388 ; Branch to use fix ISC step of $5a, i.e. 3/4 of full range
  5720.  
  5721. ;----------------------------------------------------
  5722. ; If the engine is running but we are not receiving
  5723. ; airflow sensor interrupts then
  5724. ;
  5725. ; set/reset flags
  5726. ; re-init iscYn variables
  5727. ; re-init updIscStableTimer
  5728. ; use a fixed ISC step of $3a
  5729. ;----------------------------------------------------
  5730. L1383 andm iscFlags0, #$f7 ; Assume we reset 00001000, updated below
  5731. brclr state1, #$02, L1384 ; Bail if pulse accumulator interrupts are being received
  5732. brset state1, #$11, L1384 ; Bail if notRotating or startingToCrank
  5733. jsr iscYnInit ; Init variables
  5734. andm iscFlags0, #$40 ; reset max calibration flag? 0100 0000
  5735. orm iscFlags0, #$08 ; set 0000 1000
  5736. ldab #$50 ;
  5737. jsr updIscStableTimer ; re-init updIscStableTimer
  5738. ldab #$3a ; b = $3a
  5739. bra L1388 ; Branch to use fix ISC step of $3a
  5740.  
  5741. ;---------------------------------------------------
  5742. ; Section to check for idle speed adjustment mode,
  5743. ; i.e. both ECU test mode terminal grounded and
  5744. ; timing adjustment terminal grounded
  5745. ;---------------------------------------------------
  5746. L1384 andm iscFlags0, #$fb ; Reset 00000010
  5747. ldaa port4Snap ; a = port4Snap
  5748. anda #$18 ; Keep only ECU test mode terminal grounded & timing adjustment terminal grounded
  5749. cmpa #$18 ; ECU test mode terminal grounded & timing adjustment terminal grounded
  5750. bne L1385 ; Bail if not both of them grounded
  5751. brset port3Snap1, #$04, L1385 ; Both terminal grounded, bail if car is moving
  5752. #ifdef E932
  5753. brclr port3Snap1, #$20, L1385 ; Bail if not in park/neutral???
  5754. #endif
  5755. brclr state1, #$11, L1386 ; branch if notRotating and startingToCrank clear
  5756.  
  5757. ;------------------------------------------------------------
  5758. ; Engine is either notRotating or startingToCrank
  5759. ; Reset timer (always done when both terminal not grounded)
  5760. ;------------------------------------------------------------
  5761. L1385 ldaa #$08 ; 0.2s
  5762. staa T40_iSpAdj ;
  5763.  
  5764. ;--------------------------------------------------
  5765. ; If Timer T40_iSpAdj is expired (0.2 sec after
  5766. ; both terminals grounded) we are in basic idle
  5767. ; speed adjustment mode. Branch accordingly...
  5768. ;--------------------------------------------------
  5769. L1386 ldaa T40_iSpAdj ;
  5770. bne L1390 ; Branch to next section if timer not expired
  5771.  
  5772. ;----------------------------------------------------
  5773. ; Timer is expired, we are in basic idle
  5774. ; speed adjustment mode...
  5775. ;
  5776. ; Just compute iscStepTarg (target idle speed) as a
  5777. ; function of temperature and barometric pressure,
  5778. ; reset iscLrnFlags and iscStStall and exit from subroutine
  5779. ;----------------------------------------------------
  5780. orm iscFlags0, #$04 ; Set flag
  5781. ldab #$50 ;
  5782. jsr updIscStableTimer ;
  5783. ldx #t_iscStEct0 ; x points to iscStepCurr(as a function of ECT) table
  5784. jsr interpEct ; b = t_iscStEct0(ect), basic isc value we want as a function of ECT
  5785. addb iscStBarOff ; b = t_iscStEct0(ect) + iscStBarOff, Add an offset to compensate for barometric pressure
  5786. bcc L1388 ; Branch if no overflow
  5787. ldab #$ff ; Use max
  5788. L1388 jsr iscStepMaxFunc ; Apply maximum to calculated value
  5789. stab iscStepTarg ; iscStepTarg = t_iscStEct0(ect) + iscStBarOff
  5790. L1389 clra ;
  5791. staa iscLrnFlags ; iscLrnFlags = 0
  5792. staa iscStStall ; iscStStall = 0
  5793. jmp L1431 ; Bail of subroutine
  5794.  
  5795. ;------------------------------------------------
  5796. ; Basic idle speed adjustment mode is not active
  5797. ; Section to update ISC stuff, long...
  5798. ;------------------------------------------------
  5799. L1390 brset state1, #$11, L1391 ; Branch if notRotating or startingToCrank
  5800.  
  5801. ;--------------------------------------
  5802. ; Engine is running, set /reset flags
  5803. ;--------------------------------------
  5804. andm iscLrnFlags, #$fe ; Reset bit 0
  5805. orm iscFlags1, #$01 ; Set flag indicating "normal running mode"??
  5806. bra L1392 ; Branch to continue
  5807.  
  5808. ;----------------------------------
  5809. ; notRotating or startingToCrank
  5810. ;----------------------------------
  5811. L1391 ldab #$01 ;
  5812. stab iscLrnFlags ; Reset all bit to 0 and set bit 0 to 1
  5813.  
  5814. ;---------------------------------------------------------------
  5815. ; Reset iscFlags1.0 if key in start and iscStTargSpec = iscStepCurr??
  5816. ;---------------------------------------------------------------
  5817. brset port3Snap1, #$40, L1392 ; Branch if key is not is start
  5818. ldaa iscStTargSpec ;
  5819. cmpa iscStepCurr ;
  5820. bne L1392 ; Branch if iscStTargSpec != iscStepCurr
  5821. andm iscFlags1, #$fe ;
  5822.  
  5823. ;------------------------------------------------------------------
  5824. ; Init T40_acOnTrans to 0.1sec if A/C switch was just turned on?
  5825. ;------------------------------------------------------------------
  5826. L1392 ldaa port3Snap1 ;
  5827. eora oldP3Snap1 ;
  5828. bita #$10 ;
  5829. beq L1393 ; Branch if A/C switch did not change state
  5830. brclr port3Snap1, #$10, L1394 ; Branch if A/C switch is on
  5831. L1393 .equ $
  5832. #ifdef E931
  5833. bra L1395 ;
  5834. #else
  5835. bita #$20 ;
  5836. beq L1395 ;
  5837. brset port3Snap1, #$20, L1395 ;
  5838. #endif
  5839. L1394 ldab #$04 ; 0.1sec
  5840. stab T40_acOnTrans ; T40_acOnTrans = 0.1sec
  5841.  
  5842. ;--------------------------------------------------------------
  5843. ; Update iscLrnFlags.2, flag indicating engine is running too slow?
  5844. ;--------------------------------------------------------------
  5845. L1395 ldaa iscLrnFlags ; a = old iscLrnFlags
  5846. andm iscLrnFlags, #$fb ; Assume we reset 00000100, updated below
  5847. ldab ectFiltered ;
  5848. cmpb #$3c ; 55degC
  5849. bcc L1396 ; Bail if temperature(ectFiltered) <= 55degC
  5850. ldab rpm8 ;
  5851. cmpb #$40 ;
  5852. bcc L1396 ; Bail if rpm8 >=500
  5853. brset iscLrnFlags, #$01, L1396 ; Bail if engine is notRotating or startingToCrank (and basic idle speed adjustment mode is off)
  5854. tst T40_acOnTrans ;
  5855. bne L1396 ; Bail if timer not expired
  5856.  
  5857. ;----------------------------------------------------------------------------------
  5858. ; At this point
  5859. ; temperature(ectFiltered) > 55degC
  5860. ; rpm8 < 500rpm
  5861. ; engine is running
  5862. ; T40_acOnTrans is expired (A/C switch was turned on more than 0.1sec ago, or never...)
  5863. ;
  5864. ; Set iscLrnFlags.2 indicating the engine is running to slow
  5865. ; (even though A/C transcient was ignored for 0.1sec)
  5866. ;
  5867. ; Clear T40_checkTargRpm if this condition was just detected in order to perform
  5868. ; current versus target rpm comparison right away and adjust isc step before
  5869. ; the engine stalls...
  5870. ;----------------------------------------------------------------------------------
  5871. orm iscLrnFlags, #$04 ; set 0000 0100
  5872. anda #$04 ; a = old iscLrnFlags & 0000 0100
  5873. bne L1396 ; Bail if old iscLrnFlags.2 was set
  5874. clr T40_checkTargRpm ; old iscLrnFlags.2 was not set, reset timer to trigger fast rpm update...
  5875.  
  5876. ;---------------------------------
  5877. ; Update updIscStableTimer if ???
  5878. ;---------------------------------
  5879. L1396 brclr iscLrnFlags, #$20, Ne301 ; Branch if
  5880. ldab #$50 ;
  5881. jsr updIscStableTimer ;
  5882. Ne301 .equ $
  5883.  
  5884. ;--------------------------------------------
  5885. ; Update updIscStableTimer if ??? for E931
  5886. ;--------------------------------------------
  5887. #ifdef E931
  5888. brclr ftrimFlags, #$08, L1397 ; Branch if not (speed>24km/h and port3.0 is set???)
  5889. ldab #$50 ; speed>24km/h and port3.0 is set, update timer
  5890. jsr updIscStableTimer ;
  5891. #endif
  5892.  
  5893. ;-------------------------------------------------------------------
  5894. ; Section to update iscLrnFlags.4, flag indicating that conditions are
  5895. ; good to update the isc learning variables???
  5896. ;-------------------------------------------------------------------
  5897. L1397 andm iscLrnFlags, #$ef ; Assume we reset iscLrnFlags.4 (0001 0000)
  5898. ldaa port3Snap1 ;
  5899. bpl L1398 ; Branch if idle position switch is off
  5900. brset port3Snap1, #$04, L1398 ; Branch if car is moving
  5901. ldaa T40s_iscStable ;
  5902. bne L1398 ; Branch if isc not stable yet
  5903. ldaa rpm8 ;
  5904. cmpa #$2a ;
  5905. bcc L1399 ; Branch if rpm8 >= 328rpm
  5906. L1398 brclr state1, #$01, L1400 ; Branch if startingToCrank clear
  5907. brset iscFlags1, #$01, L1400 ; Branch if not (key in start and iscStTargSpec = iscStepCurr)
  5908.  
  5909. ;--------------------------------------------
  5910. ; At this point
  5911. ; car is not moving
  5912. ; and idle switch is on
  5913. ; and isc is stable
  5914. ; and rpm8 >= 328
  5915. ;
  5916. ; or these conditions are not met but
  5917. ; engine is startingToCrank
  5918. ; and (key in start and iscStTargSpec = iscStepCurr)
  5919. ;
  5920. ; Set iscLrnFlags.4 indicating we can update the isc
  5921. ; learning variables?
  5922. ;--------------------------------------------
  5923. L1399 orm iscLrnFlags, #$10 ;
  5924. bra L1401 ;
  5925.  
  5926. ;-----------------------------------------------------
  5927. ; At this point,
  5928. ; car is moving
  5929. ; or idle position switch is off
  5930. ; or isc not yet stable
  5931. ; or rpm < 328
  5932. ;
  5933. ; and engine is startingToCrank or key is not
  5934. ; in start or iscStTargSpec != iscStepCurr
  5935. ;
  5936. ; Re-init T40_iscLrn to 5sec
  5937. ; Conditions are not good to update ISC learning variables?
  5938. ; Flag is already reset so just reset that timer so that
  5939. ; we have a 5sec delay when we can go back to being able
  5940. ; to update them...
  5941. ;--------------------------------------------------------------
  5942. L1400 ldab #$c8 ; 5sec
  5943. stab T40_iscLrn ; T40_iscLrn = 5sec
  5944.  
  5945. ;---------------------------------------------------------------------------------------------------
  5946. ; Check whether abs(idleSpdTarg - rpm8) > (5/256 * idleSpdTarg)
  5947. ;
  5948. ; Basically chech if target and current rpm are more than 2% apart
  5949. ;---------------------------------------------------------------------------------------------------
  5950. L1401 brclr iscLrnFlags, #$10, L1403 ; Bail if conditions are not good to update isc variables
  5951. ldaa T40_checkTargRpm ;
  5952. bne L1403 ; Bail if timer not expired
  5953. ldaa idleSpdTarg ; a = idleSpdTarg
  5954. suba rpm8 ; a = idleSpdTarg - rpm8
  5955. ror temp2 ; shift sign (carry) of result in temp2.7
  5956. bpl L1402 ; Branch if (idleSpdTarg - rpm8) >= 0
  5957. nega ; a = abs(idleSpdTarg - rpm8)
  5958. L1402 psha ; st0 = abs(idleSpdTarg - rpm8)
  5959. ldaa idleSpdTarg ; a = idleSpdTarg
  5960. ldab #$0a ; b = $0a
  5961. mul ; d = $0a * idleSpdTarg
  5962. pulb ; b = abs(idleSpdTarg - rpm8); a = $0a/256 * idleSpdTarg
  5963. cba ;
  5964. bcs L1404 ; Branch if ($0a/256 * idleSpdTarg) < abs(idleSpdTarg - rpm8), basically branch if difference between current rpm and desired one is too high
  5965. L1403 jmp L1416 ; Jump to next section
  5966.  
  5967. ;------------------------------------------------------------------
  5968. ; At this point abs(idleSpdTarg - rpm8) > (5/256 * idleSpdTarg)
  5969. ; or equivalently abs(idleSpdTarg - rpm8)/idleSpdTarg > 2%
  5970. ; i.e. Target and current rpm are more than 2% apart
  5971. ;
  5972. ; Section to update iscYn (until L1415).
  5973. ;
  5974. ; Basically this section increases the current iscYn (iscY1, iscY2
  5975. ; or iscY3) if the current rpm is lower than the target and vice-versa
  5976. ; iscYn is centered at $80 (100%, no correction). The isc step used
  5977. ; is increase/decreased by iscYn-$80 later in the code
  5978. ;------------------------------------------------------------------
  5979. L1404 aslb ; b = 2 * abs(idleSpdTarg - rpm8)
  5980. bcs L1405 ; branch to use max of $ff if overflow
  5981. aslb ; b = 4 * abs(idleSpdTarg - rpm8)
  5982. bcc L1406 ; Branch if no overflow
  5983. L1405 ldab #$ff ; use max of $ff
  5984. L1406 stab temp1 ; temp1 = 4 * abs(idleSpdTarg - rpm8)
  5985. ldx #L2035 ; x point to L2035
  5986. ldaa temp1 ; a = 4 * abs(idleSpdTarg - rpm8)
  5987. jsr interp32 ; b = L2035(4 * abs(idleSpdTarg - rpm8))
  5988. stab temp1 ; temp1 = L2035(4 * abs(idleSpdTarg - rpm8))
  5989. jsr iscPointers ; have x point to isc0 or isc1 and have y point to iscY0, iscY1 or iscY2
  5990. ldab $00,y ; b = iscYn, y = y + 1
  5991. decy ; y = y-1, points back to same value
  5992. ldaa temp2 ; a.7 = sign(carry) of idleSpdTarg - rpm8 (see above L1402)
  5993. bpl L1410 ; Branch if idleSpdTarg - rpm8 was positive
  5994.  
  5995. ;---------------------------------------------------------------------------
  5996. ; idleSpdTarg - rpm8 was negative. i.e. current rpm is too high
  5997. ; Compute b = iscYn - min (iscStepTarg, L2035(4 * abs(idleSpdTarg - rpm8)))
  5998. ;---------------------------------------------------------------------------
  5999. ldaa iscStepTarg ; a = iscStepTarg
  6000. beq L1416 ; Branch if iscStepTarg = 0
  6001. cmpa temp1 ;
  6002. bhi L1408 ; Branch if iscStepTarg > L2035(4 * abs(idleSpdTarg - rpm8))
  6003. staa temp1 ; Use max of = iscStepTarg
  6004. L1408 subb temp1 ; b = iscYn - min (iscStepTarg, L2035(4 * abs(idleSpdTarg - rpm8)))
  6005. bcc L1409 ; Branch if no underflow
  6006. clrb ; Use min of 0
  6007. L1409 bra L1412 ;
  6008.  
  6009. ;-------------------------------------------------------------------------------------
  6010. ; idleSpdTarg - rpm8 was positive, i.e. current rpm is too low
  6011. ; Compute b = iscYn + min(iscStepMax - iscStepTarg, L2035(4 * abs(idleSpdTarg - rpm8))
  6012. ;-------------------------------------------------------------------------------------
  6013. L1410 ldaa iscStepMax ; a = iscStepMax
  6014. suba iscStepTarg ; a = iscStepMax - iscStepTarg
  6015. beq L1416 ; Branch if iscStepTarg = iscStepMax
  6016. cmpa temp1 ;
  6017. bhi L1411 ; Branch if iscStepMax - iscStepTarg > L2035(4 * abs(idleSpdTarg - rpm8))
  6018. staa temp1 ; Use max of iscStepMax - iscStepTarg
  6019. L1411 addb temp1 ; b = iscYn + min(iscStepMax - iscStepTarg, L2035(4 * abs(idleSpdTarg - rpm8))
  6020. bcc L1412 ; Branch if no overflow
  6021. ldab #$ff ; Use max of $ff
  6022.  
  6023. ;-------------------------------------------------------------------------------------
  6024. ; at this point
  6025. ; b = iscYn - min(iscStepTarg, L2035(4 * abs(idleSpdTarg - rpm8)))
  6026. ; or
  6027. ; b = iscYn + min(iscStepMax - iscStepTarg, L2035(4 * abs(idleSpdTarg - rpm8))
  6028. ;
  6029. ; Restart T40_checkTargRpm to 1 sec and decide whether to apply min/max to new iscYn=b value
  6030. ;-------------------------------------------------------------------------------------
  6031. L1412 ldaa #$28 ; 1sec
  6032. staa T40_checkTargRpm ; T40_checkTargRpm = 1sec
  6033. brset iscLrnFlags, #$01, L1413 ; Branch to min/max checking if engine is notRotating or startingToCrank
  6034. bra L1415 ; Branch to store b in iscYn, Skip min/max checking
  6035. nop ;
  6036.  
  6037. ;-------------------------------------------------
  6038. ; Check b for min and max and then store in iscYn
  6039. ;-------------------------------------------------
  6040. L1413 cmpb #$86 ;
  6041. bcs L1414 ; Branch if b < $86
  6042. ldab #$86 ; Use max of $86
  6043. L1414 cmpb #$7b ;
  6044. bcc L1415 ; Branch if b >= $7b
  6045. ldab #$7b ; Use min of $7b
  6046. L1415 stab $00,y ; iscYn = b....
  6047.  
  6048.  
  6049. ;---------------------------------------------------------------
  6050. ; re-init iscYn variables if car is moving and rpm8>=1000rpm
  6051. ;---------------------------------------------------------------
  6052. L1416 brclr port3Snap1, #$04, L1417 ; Branch if car is not moving
  6053. ldaa rpm8 ;
  6054. cmpa #$80 ;
  6055. bcs L1417 ; Branch if rpm8 < 1000rpm
  6056. jsr iscYnInit ;
  6057.  
  6058. ;--------------------------------------------------------
  6059. ; Transfer iscY0 to iscY2 if power steering pump is off???
  6060. ;--------------------------------------------------------
  6061. L1417 brset port3Snap1, #$08, L1418 ; Bail if power steering pump is activated
  6062. ldaa iscY0 ;
  6063. staa iscY2 ; iscY2 = iscY0
  6064.  
  6065. ;---------------------------------------------------------
  6066. ; Section to compute iscStepTarg from various information
  6067. ; Ends at L1428
  6068. ;---------------------------------------------------------
  6069. ;-------------------------------------------------------------------------
  6070. ; Compute workIscStep = b = max(iscStBase, iscStBaseAc, iscStBaseCSt)
  6071. ;-------------------------------------------------------------------------
  6072. L1418 ldab iscStBase ; b = iscStBase
  6073. cmpb iscStBaseAc ;
  6074. bcc L1419 ; Branch if iscStBase >= iscStBaseAc
  6075. ldab iscStBaseAc ; Use min of iscStBaseAc
  6076. L1419 cmpb iscStBaseCSt ;
  6077. bcc Ne3cd ; Branch if max(iscStBase, iscStBaseAc) >= iscStBaseCSt
  6078. ldab iscStBaseCSt ; Use min of iscStBaseCSt
  6079. Ne3cd .equ $
  6080.  
  6081. ;-----------------------------------------------------------------
  6082. ; At this point b = workIscStep
  6083. ;
  6084. ; Take into port3.0 signal for E931?????
  6085. ; Basically continue calculating the max isc step to use,
  6086. ; this time check A/C and port3.0
  6087. ;-----------------------------------------------------------------
  6088. #ifdef E931
  6089. brclr ftrimFlags, #$08, L1420 ; Bail if not (speed>24km/h and port3.0 set)
  6090. ldaa #$53 ; Use $53
  6091. brset port3Snap1, #$10, Me3db ; Branch if A/C switch is off
  6092. ldaa #$78 ; Use higher value since A/C is on
  6093. Me3db cba ;
  6094. bcs L1420 ; Branch if we already have max value, i.e. workIscStep > a
  6095. tab ; Use new max value
  6096. #endif
  6097.  
  6098. ;---------------------------------------------
  6099. ; At this point b = workIscStep
  6100. ; Compensate for barometric pressure and
  6101. ;---------------------------------------------
  6102. L1420 clra ; a = 0
  6103. addb iscStBarOff ; d = workIscStep + iscStBarOff
  6104. adca #$00 ; propagate carry
  6105.  
  6106. ;------------------------------------------------
  6107. ; At this point b = workIscStep
  6108. ; Increase iscStep according to iscStStartUsed
  6109. ;
  6110. ; i.e. isc step adjustment upon engine startup
  6111. ;------------------------------------------------
  6112. addb iscStStartUsed ; d = workIscStep + iscStBarOff + iscStStartUsed
  6113. adca #$00 ; propagate carry
  6114. jsr ovfCheck ; Check for overflow (force result to be in b with $ff max)
  6115.  
  6116. ;------------------------------------------------
  6117. ; Check workIscStep for min value in iscStStall
  6118. ;------------------------------------------------
  6119. cmpb iscStStall ;
  6120. bcc L1421 ; Branch if workIscStep + iscStBarOff + iscStStartUsed >= iscStStall
  6121. ldab iscStStall ; Use min of iscStStall
  6122. bra L1422 ; Branch to continue
  6123.  
  6124. ;-------------------------------------------------------------------
  6125. ; At this point workIscStept + iscStBarOff + iscStStartUsed >= iscStStall
  6126. ;
  6127. ; i.e. what we are using is already higher
  6128. ; than the minimum we therefore don't need
  6129. ; that minimum anymore...
  6130. ;
  6131. ; Reset iscStStall to 0 and reset flag
  6132. ;-------------------------------------------------------------------
  6133. L1421 clr iscStStall ; iscStStall = 0
  6134. andm iscLrnFlags, #$df ; Reset 00100000
  6135.  
  6136. ;---------------------------------------------------------------------------------
  6137. ; At this point, b = workIscStep
  6138. ; Add the effect of power steering pump
  6139. ;---------------------------------------------------------------------------------
  6140. L1422 brclr port3Snap1, #$08, L1423 ; Branch if power steering pump is off
  6141. #ifdef E931
  6142. addb #$0f ; b = workIscStep + $0f
  6143. #else
  6144. addb #$11 ;
  6145. #endif
  6146. bcc L1423 ; Branch if no overflow
  6147. ldab #$ff ; Use max of $ff
  6148.  
  6149. ;---------------------------------------------------------------
  6150. ; increase the iscStep if the engine is running too slow
  6151. ; (stall conditions?) and conditions are not good to update
  6152. ; isc variables (to avoid getting confused with cranking maybe?)
  6153. ;---------------------------------------------------------------
  6154. L1423 brclr iscLrnFlags, #$04, L1424 ; Branch if engine is not running too slow
  6155. brset iscLrnFlags, #$10, L1424 ; Branch if conditions are good to update isc variables
  6156. addb #$22 ; b = workIscStep + $22
  6157. bcc L1424 ; Branch if no overflow
  6158. ldab #$ff ; Use max of $ff
  6159.  
  6160. ;-------------------------------------------------------------------
  6161. ; Compute temp3 = workIscStep + (iscm/256 - $80) + (iscYn - $80)
  6162. ; workIscStep'+ iscStBarOff + (iscm/256 - $80) + (iscYn - $80)
  6163. ; This is isc step we are going to use if engine is running
  6164. ;-------------------------------------------------------------------
  6165. L1424 jsr iscCalc3 ; b = workIscStep + (iscm/256 - $80)
  6166. jsr iscCalc4 ; b = workIscStep + (iscm/256 - $80) + (iscYn - $80)
  6167. stab temp3 ; temp3 = workIscStep + (iscm/256 - $80) + (iscYn - $80)
  6168.  
  6169. ;---------------------------------------------------------------
  6170. ; Compute temp2 = L2031(ect) + iscStBarOff + (iscm/256 - $80)
  6171. ; This is isc step we are going to use if the engine is notRotating
  6172. ;---------------------------------------------------------------
  6173. ldx #L2031 ; x points to L2031
  6174. jsr interpEct ; b = L2031(ect)
  6175. addb iscStBarOff ; b = L2031(ect) + iscStBarOff
  6176. bcc L1425 ; Branch if no overflow
  6177. ldab #$ff ; overflow, use max
  6178. L1425 jsr iscCalc3 ; b = L2031(ect) + iscStBarOff + (iscm/256 - $80)
  6179. stab temp2 ; temp2 = L2031(ect) + iscStBarOff + (iscm/256 - $80)
  6180.  
  6181. ;-----------------------------------------------------------------------------
  6182. ; Compute iscStTargSpec = temp2 + (iscYn - $80)
  6183. ; = L2031(ect) + iscStBarOff + (iscm/256 - $80) + (iscYn - $80)
  6184. ;
  6185. ; This is isc step we are going to use if engine is starting to crank
  6186. ;-----------------------------------------------------------------------------
  6187. jsr iscCalc4 ; b = L2031(ect) + iscStBarOff + (iscm/256 - $80) + (iscYn - $80)
  6188. jsr iscStepMaxFunc ; apply max to b
  6189. stab iscStTargSpec ; iscStTargSpec = L2031(ect) + iscStBarOff + (iscm/256 - $80) + (iscYn - $80)
  6190.  
  6191. ;-----------------------------------------------------------------------
  6192. ; At this point,
  6193. ;
  6194. ; temp3 = workIscStep' + iscStBarOff + (iscm/256 - $80) + (iscYn - $80)
  6195. ; b = L2031(ect) + iscStBarOff + (iscm/256 - $80) + (iscYn - $80)
  6196. ; temp2 = L2031(ect) + iscStBarOff + (iscm/256 - $80)
  6197. ;
  6198. ; Now decide which value we are going to use
  6199. ; as working isc step either b, temp2 or temp3
  6200. ; Not sure why engine state is taken from a mix of state1 and iscLrnFlags???
  6201. ;-----------------------------------------------------------------------
  6202. brclr iscLrnFlags, #$01, L1427 ; Branch to use temp3 if engine is not (notRotating or startingToCrank), runnning, normally or not...
  6203. brclr state1, #$10, L1428 ; Branch to use iscStTargSpec (already loaded in b) if notRotating clear, only startingToCrank left???
  6204. ldab temp2 ; notRotating, use temp2
  6205. bra L1428 ;
  6206. L1427 ldab temp3 ; use temp3
  6207.  
  6208. ;-------------------------------------------------------------------------
  6209. ; At this point b contains the working isc step that we have been
  6210. ; updating/calculating for a while now, apply a max to it and store it
  6211. ; in iscStepTarg, this is the is isc step target...
  6212. ;-------------------------------------------------------------------------
  6213. L1428 jsr iscStepMaxFunc ; apply max to b
  6214. stab iscStepTarg ; iscStepTarg
  6215.  
  6216. ;--------------------------------------------
  6217. ; Section to update iscYn and iscm variables
  6218. ;--------------------------------------------
  6219. ;------------------------------------------------------------------------------
  6220. ; Check if a bunch of conditions are met to update the isc learning variables
  6221. ;------------------------------------------------------------------------------
  6222. brclr iscLrnFlags, #$10, L1431 ; Bail if conditions are not good to update isc variables
  6223. ldab iscStepMax ; b = iscStepMax
  6224. ldaa iscStepTarg ; a = iscStepTarg
  6225. beq L1431 ; bail if iscStepTarg = 0
  6226. cba ;
  6227. beq L1431 ; bail if iscStepTarg = iscStepMax
  6228. brset port3Snap1, #$08, L1431 ; bail if power steering pump is on
  6229. brset iscLrnFlags, #$01, L1431 ; bail if notRotating or startingToCrank
  6230. ldaa T0p5_crCold ; a = T0p5_crCold
  6231. bne L1431 ; bail if timer not expired
  6232. ldaa ectFiltered ; a = ectFiltered
  6233. cmpa #$1c ; 86degC
  6234. bcc L1431 ; bail if temperature(ectFiltered) <= 86degC
  6235. brset port4Snap, #$10, L1431 ; Bail if timing terminal grounded
  6236. ldaa iscStStartUsed ; a = iscStStartUsed
  6237. bne L1431 ; Bail if iscStStartUsed != 0
  6238. brclr port5, #$10, L1431 ; Bail if fuel pressure solenoid activated
  6239.  
  6240. ;-----------------------------------------------------------
  6241. ; All the conditions are met, update iscm and iscYn at 1 Hz
  6242. ;-----------------------------------------------------------
  6243. ;------------------------------------------
  6244. ; First check if time has come to update
  6245. ; variables and then re-init timer
  6246. ;------------------------------------------
  6247. ldaa T40_iscLrn ; a = T40_iscLrn
  6248. bne L1431 ; Bail to exit subr. if timer T40_iscLrn not expired (time has not come yet...)
  6249. ldaa #$28 ; Timer is expired, a = 1sec
  6250. staa T40_iscLrn ; re-init T40_iscLrn to 1 sec
  6251.  
  6252. ;-----------------------------------------
  6253. ; Get current pointers and compute
  6254. ; newIscm = old iscm + 3 * (iscYn - $80)
  6255. ;-----------------------------------------
  6256. jsr iscPointers ; have x point to isc0 or isc1 and have y point to iscY0, iscY1 or iscY2
  6257. ldd #$0180 ; d = $0180
  6258. std temp2 ; temp2:temp3 = $0180
  6259. ldaa #$03 ; a = $03
  6260. ldab $00,y ; b = iscYn, y = y + 1
  6261. decy ; y = y - 1
  6262. mul ; d = 3 * iscYn
  6263. subd temp2 ; d = 3 * (iscYn - $80)
  6264. addd $00,x ; d = iscm + 3 * (iscYn - $80)
  6265. jsr iscMinMax ; Apply min and max to d
  6266. std temp1 ; temp1:temp2 = newIscm = iscm + 3 * (iscYn - $80)
  6267.  
  6268. ;---------------------------------------------------
  6269. ; Compute newIscYn = newIscm + 3 * (iscYn - $80)
  6270. ;---------------------------------------------------
  6271. tab ; b = newIscm/256
  6272. ldaa $00,y ; a = iscYn, y = y + 1
  6273. decy ; y = y - 1
  6274. subb $00,x ; b = newIscm/256 - iscm/256
  6275. bcc L1429 ; Branch to continue if newIscm >= old iscm
  6276.  
  6277. ;---------------------
  6278. ; newIscm < old iscm
  6279. ;---------------------
  6280. negb ; b = (iscm - newIscm)/256
  6281. aba ; a = iscYn + (iscm - newIscm)/256
  6282. bcc L1430 ; Branch if no overflow
  6283. ldaa #$ff ; Use max of $ff
  6284. bra L1430 ; Branch to store
  6285.  
  6286. ;----------------------
  6287. ; newIscm >= old iscm
  6288. ;----------------------
  6289. L1429 sba ; a = iscYn - (newIscm - iscm)/256
  6290. bcc L1430 ; branch if no underflow
  6291. clra ; Use min of 0
  6292.  
  6293. ;---------------------------------------------------------
  6294. ; At this point
  6295. ; a = newIscYn
  6296. ; [temp1:temp2] = newIscm
  6297. ;
  6298. ; Where
  6299. ;
  6300. ; newIscm = oldIscm + 3 * (oldIscYn - $80)
  6301. ;
  6302. ; | oldIscYn + (oldIscm - newIscm)/256 if newIscm < oldIscm
  6303. ; newIscYn = |
  6304. ; | oldIscYn - (newIscm - oldIscm)/256 if newIscm >= oldIscm
  6305. ;
  6306. ;---------------------------------------------------------
  6307. L1430 staa $00,y ; Update iscYn with new value
  6308. ldd temp1 ; d = newIscm
  6309. std $00,x ; Update iscm with new value
  6310. L1431 rts ;
  6311.  
  6312.  
  6313.  
  6314. ;******************************************************************
  6315. ;
  6316. ; ISC step calculation
  6317. ;
  6318. ; input: A = step
  6319. ; output: A =(~step & 7F)
  6320. ;
  6321. ; (~step & 7F) stored in iscStepCom
  6322. ; step stored in iscStepCurr
  6323. ;
  6324. ;
  6325. ;******************************************************************
  6326. iscStepComp pshb ; st0 = val
  6327. tab ; b = step
  6328. coma ; a = ~step
  6329. anda #$7f ; a = ~step & $7f
  6330. std iscStepCom ; iscStepCom = ~step & $7f, iscStepCurr = step
  6331. pulb ; b = val
  6332. rts ;
  6333.  
  6334.  
  6335.  
  6336. ;******************************************************************
  6337. ;
  6338. ; Increase the value of T40s_iscStable timer if the
  6339. ; new value is higher than the current one
  6340. ;
  6341. ; T40s_iscStable = max(T40s_iscStable, b)
  6342. ;
  6343. ;******************************************************************
  6344. updIscStableTimer
  6345. cmpb T40s_iscStable ;
  6346. bcs L1434 ; Branch if b < T40s_iscStable
  6347. stab T40s_iscStable ; Use new higher value
  6348. L1434 rts ;
  6349.  
  6350.  
  6351.  
  6352. ;******************************************************************
  6353. ;
  6354. ;
  6355. ; b = min(b,iscStepMax)
  6356. ;
  6357. ;
  6358. ;******************************************************************
  6359. iscStepMaxFunc cmpb iscStepMax
  6360. bcs L1436
  6361. ldab iscStepMax
  6362. L1436 rts
  6363.  
  6364.  
  6365.  
  6366. ;******************************************************************
  6367. ;
  6368. ; Input:
  6369. ; b = val1
  6370. ;
  6371. ;
  6372. ;******************************************************************
  6373. iscCalc3 stab temp1 ; temp1 = val1
  6374. bsr iscPointers ; x points to iscm; y points to iscYn
  6375. ldd $00,x ; d = iscm
  6376. bsr iscMinMax ; apply min and max, d = ...
  6377. tab ; b = iscm/256
  6378. clra ; a = 0
  6379. addb temp1 ; b = iscm/256 + val1
  6380. rola ; propagate carry, d = iscm/256 + val1
  6381. bra L1445 ; go to subtract $80 with min check and then make sure result fits in b (max of $ff)
  6382.  
  6383.  
  6384. ;******************************************************************
  6385. ;
  6386. ; ISC step related, apply min and max to D
  6387. ;
  6388. ; Input:
  6389. ; d = val
  6390. ; Output:
  6391. ; d = max($6c00, min($b000, val))
  6392. ;******************************************************************
  6393. iscMinMax cmpd #$b000 ;
  6394. bcs L1439 ; Branch if d < $b000
  6395. ldd #$b000 ; Use max of $b000
  6396. L1439 cmpd #$6c00 ;
  6397. bcc L1440 ; Branch if d >= $6c00
  6398. ldd #$6c00 ; Use min of $6c00
  6399. L1440 rts ;
  6400.  
  6401.  
  6402.  
  6403. ;******************************************************************
  6404. ;
  6405. ; Get current pointers to ISC step learning variables
  6406. ;
  6407. ; input: none (port3Snap1 is used)
  6408. ; output: X points to isc0 if A/C switch off, isc1 otherwise
  6409. ; Y points to iscY0, iscY1 or iscY2
  6410. ;
  6411. ; A/C switch PS pump x y
  6412. ; off off isc0 iscY0
  6413. ; off on isc0 iscY2
  6414. ; on off isc1 iscY1
  6415. ; on on isc1 iscY2
  6416. ;
  6417. ;******************************************************************
  6418. iscPointers ldx #isc0 ; x points to isc0
  6419. ldy #iscY0 ; y points to iscY0
  6420. brset port3Snap1, #$10, L1442 ; Branch if A/C switch off
  6421. inx ;
  6422. inx ; x points to isc1
  6423. incy ; y points to iscY1
  6424. L1442 brclr port3Snap1, #$08, L1443 ; Branch if power steering pump is deactivated?
  6425. ldy #iscY2 ; y points to iscY2
  6426. L1443 rts ;
  6427.  
  6428.  
  6429.  
  6430. ;******************************************************************
  6431. ;
  6432. ; Input:
  6433. ; b = val
  6434. ;
  6435. ;
  6436. ;******************************************************************
  6437. iscCalc4 bsr iscPointers ; x points to iscm; y points to iscYn
  6438. clra ; a = 0
  6439. addb $00,y ; d = val + iscYn
  6440. adca #$00 ; propagate carry
  6441. L1445 subd #$0080 ; d = val + iscYn -$80
  6442. bcc L1446 ; Branch if no underflow
  6443. clra ;
  6444. clrb ; Use min of 0
  6445. L1446 jmp ovfCheck ; Check that result fits in b ($ff max)
  6446.  
  6447.  
  6448.  
  6449.  
  6450. ;******************************************************************
  6451. ;
  6452. ; Initialize ISC iscYn variables
  6453. ;
  6454. ; E931:
  6455. ;
  6456. ; iscY0 = $86
  6457. ; iscY1 = $8a
  6458. ; iscY2 = $86
  6459. ;
  6460. ; E932:
  6461. ;
  6462. ; iscY0 = $86 or iscY0 = $83
  6463. ; iscY1 = $8a iscY1 = $83
  6464. ; iscY2 = $86 iscY2 = $83
  6465. ;
  6466. ;******************************************************************
  6467. iscYnInit .equ $
  6468. #ifdef E931
  6469. ldaa #$86
  6470. ldab #$8a
  6471. #else
  6472. ldaa #$83
  6473. tab
  6474. brclr port3Snap1, #$20, L1448
  6475. ldaa #$86
  6476. ldab #$8a
  6477. #endif
  6478.  
  6479. L1448 staa iscY0
  6480. staa iscY2
  6481. stab iscY1
  6482. rts
  6483.  
  6484.  
  6485.  
  6486. ;******************************************************************
  6487. ;
  6488. ;
  6489. ; Sensor check related table,
  6490. ; correspond one for one to table at t_snsrChk
  6491. ; Each entry is the bit to set/reset in the faulth:faultl
  6492. ; for the corresponding sensor
  6493. ;
  6494. ;
  6495. ;******************************************************************
  6496. t_snsrChkBit .word $0200, $4000, $8000, $0001
  6497. .word $0008, $0010, $0040, $2000
  6498. .word $0004, $0400, $0800, $0002
  6499.  
  6500.  
  6501.  
  6502. ;****************************************************************
  6503. ;
  6504. ; Used for output of error codes to test connector
  6505. ;
  6506. ; in order: o2 maf iat tps N/A ect cas tdc (high fault, $01, $02, $04, $08, $10, $20, $40, $80)
  6507. ; vss bar knk inj fuel egr coil N/A (low fault, $01, $02, $04, $08, $10, $20, $40, $80)
  6508. ;
  6509. ; Format:
  6510. ;
  6511. ; high nibble = number of "long pulse" to output (max of 7?)
  6512. ; low nibble = number of "short pulse" to output (max of 15?)
  6513. ;
  6514. ;****************************************************************
  6515. t_snsrChkCode .byte $11, $12, $13, $14, $15, $21, $22, $23, $24, $25, $31, $41, $42, $43, $44, $00
  6516.  
  6517.  
  6518.  
  6519. ;****************************************************************
  6520. ;
  6521. ; Sensor check subroutine vectors
  6522. ;
  6523. ;****************************************************************
  6524. t_snsrChk .word test_maf, test_cas, test_tdc, test_reed
  6525. .word test_inj, test_fpump, test_coil, test_ect
  6526. .word test_knock, test_iat, test_tps, test_baro
  6527.  
  6528.  
  6529.  
  6530. ;****************************************************************
  6531. ;
  6532. ; Actuator activate lookup table (OBD command processing)
  6533. ;
  6534. ;****************************************************************
  6535. t_obdActMask .byte $20, $10, $08, $04, $01, $02
  6536.  
  6537.  
  6538.  
  6539. ;****************************************************************
  6540. ;
  6541. ; Injector disable lookup table (OBD command processing)
  6542. ;
  6543. ;****************************************************************
  6544. t_obdInjMask .byte $fb, $fd, $f7, $fe
  6545.  
  6546.  
  6547.  
  6548. ;******************************************************************
  6549. ;
  6550. ;
  6551. ; Fourth subroutine
  6552. ;
  6553. ;
  6554. ;******************************************************************
  6555. ;--------------------------------------------------
  6556. ; Reset a few things (most fault codes, etc)
  6557. ; if ECU power has been on for less than 0.5 sec
  6558. ;--------------------------------------------------
  6559. subroutine4 ldaa T2_EcuPower ;
  6560. adda #$01 ;
  6561. bcc L1454 ; Branch if its been more than 0.5sec since ECU power has been on
  6562. andm validFlags, #$f9 ; Reset o2 and egrt "sensor condition determined" flags
  6563. orm validFlags, #$01 ; set flag indicating no CAS interrupt received for a long time
  6564. andm faultHi, #$01 ; Reset all current faults but o2 sensor bit
  6565. andm faultLo, #$20 ; Reset all current faults but egrt sensor bit
  6566. ldaa #$08 ;
  6567. staa T2_snsrChk ; re-init T2_snsrChk with 4 sec
  6568. ldaa #$ff ;
  6569. staa T2_stCrank ; re-init T2_stCrank with max value (127.5sec)
  6570. andm state2, #$ef ; Reset ECT related flag???
  6571.  
  6572. ;------------------------------------
  6573. ; Set flag if engine not rotating
  6574. ;------------------------------------
  6575. L1454 ldaa T40_engRot
  6576. bne L1455 ; Branch if engine rotating
  6577. orm validFlags, #$01 ; Engine not rotating (or very slowly) set flag
  6578. bra L1458 ; Branch to continue
  6579.  
  6580. ;------------------------------------------------------
  6581. ; If the engine is startingToCrank or was not rotating
  6582. ; the last time we were here and is now rotating
  6583. ;
  6584. ; Update ectStCrank and related timers, the ect when
  6585. ; we started cranking...
  6586. ;------------------------------------------------------
  6587. L1455 brset validFlags, #$01, L1456 ; Branch if engine was not rotating the last time we checked
  6588. brclr state1, #$01, L1458 ; Bail if startingToCrank clear
  6589. L1456 andm validFlags, #$fe ; startingToCrank or was not rotating the last time we were here and is now rotating
  6590. ldaa ectFiltered ;
  6591. staa ectStCrank ; ectStCrank = ectFiltered
  6592. ldab #$ff ;
  6593. stab T2_stCrank ; T2_stCrank = $ff (127.5sec)
  6594. stab T0p5_crank2 ; T0p5_crank2 = $ff (510sec)
  6595. ldaa ectRaw ;
  6596. ldab #$5a ; 180sec
  6597. cmpa #$1c ; 86 degC
  6598. bls L1457 ; Branch if temperature(ectRaw)>= 86degC
  6599. ldab #$b4 ; 360 sec
  6600. L1457 stab egrtTimerThr ; egrtTimerThr = $5a or $b4 (180sec or 360sec depending on ect)
  6601.  
  6602. ;-------------------------------------------------
  6603. ; Section performing the sensor check functions..
  6604. ;-------------------------------------------------
  6605. ;--------------------------------------------------------------------------
  6606. ; Loop sensrChkIdx from 0 to 7 if T2_stCrank (startingToCrank) started counting
  6607. ; less than 60 sec ago (only first 8 sensor tests are performed,
  6608. ; the most important ones I suppose to start the car, except for reed
  6609. ; switch, safety maybe?) else loop at 12 (all tests are performed)
  6610. ;--------------------------------------------------------------------------
  6611. L1458 ldaa #$08 ; a = $08
  6612. ldab T2_stCrank ; b = T2_stCrank
  6613. addb #$78 ; b = T2_stCrank + $78 (60sec)
  6614. bcs L1459 ; Branch if T2_stCrank started counting less than 60 sec ago
  6615. adda #$04 ; a = $0c
  6616. L1459 ldab sensrChkIdx ; b = sensrChkIdx
  6617. cba ;
  6618. bhi L1460 ; Branch to start checking if sensrChkIdx < $0c or $08
  6619.  
  6620. ;-------------------------------------------------
  6621. ; sensrChkIdx >= $0c or $08
  6622. ; re-init T2_snsrChk to 4 sec and sensrChkIdx to 0
  6623. ;-------------------------------------------------
  6624. ldaa #$08 ;
  6625. staa T2_snsrChk ; T2_snsrChk = 8 (4 sec)
  6626. clrb ;
  6627. stab sensrChkIdx ; sensrChkIdx = 0
  6628.  
  6629. ;----------------------------------------------------------
  6630. ; Call the sensor check rountine according to sensrChkIdx
  6631. ;----------------------------------------------------------
  6632. L1460 aslb ; b = 2*sensrChkIdx (2 bytes per address...)
  6633. ldx #t_snsrChk ; x = t_snsrChk
  6634. abx ; x = t_snsrChk + 2*sensrChkIdx
  6635. ldy $00,x ; y points to sensor check function
  6636. ldx #t_snsrChkBit ; x = t_snsrChkBit (sensor "fault bit position" table)
  6637. abx ; x = t_snsrChkBit + 2*sensrChkIdx
  6638. clrb ; b = 0
  6639. jsr $00,y ; call sensor check subroutine
  6640. tstb ;
  6641. beq L1462 ; Branch if no error found
  6642. bpl L1463 ; Branch if inconclusive
  6643.  
  6644. ;------------------------------------------------------
  6645. ; Sensor check returned negative, error is detected
  6646. ;
  6647. ; Check if T2_snsrChk is expired, which would mean we have
  6648. ; been stuck on testing the same sensor for 4 sec and
  6649. ; it never worked properly...)
  6650. ;------------------------------------------------------
  6651. ldd $00,x ; [a:b] = t_snsrChkBit(sensrChkIdx)
  6652. tst T2_snsrChk ;
  6653. beq L1461 ; branch if T2_snsrChk is 0 (more than 4 sec elapsed since sensrChkIdx was reset to 0)
  6654.  
  6655. ;------------------------------------------------------
  6656. ; T2_snsrChk is not expired, don't set the error flags but
  6657. ; if it they were already set increase sensrChkIdx
  6658. ; (meaning this sensor was already detected as bad
  6659. ; with a 4 sec check, don't do it again for that
  6660. ; sensor, go to the next one)
  6661. ;------------------------------------------------------
  6662. anda faultHi ;
  6663. bne L1463 ; Branch to increase sensrChkIdx if error bit was already set (if it was located in faultHi)
  6664. andb faultLo ;
  6665. bne L1463 ; Branch to increase sensrChkIdx if error bit was alrready set (if it was located in faultLo)
  6666. bra L1464 ; Error bit was not already set, don't change sensrChkIdx (check it agaian next time)
  6667.  
  6668. ;-----------------------------------------------------------------------
  6669. ; More than 4 sec elapsed since sensrChkIdx was reset to 0
  6670. ; (this means that we have been stuck on testing the same sensor
  6671. ; for 4 sec and it never worked properly...)
  6672. ;
  6673. ; Set the error flags and increase sensrChkIdx
  6674. ;-----------------------------------------------------------------------
  6675. L1461 oraa faultHi ; Set the error bit if located in faultHi
  6676. orab faultLo ; Set the error bit if located in faultLo
  6677. std faultHi ; Update faultHi:faultLo
  6678. oraa stFaultHi ; Set the error bit if located in stFaultHi
  6679. orab stFaultLo ; Set the error bit if located in stFaultLo
  6680. std stFaultHi ; Update stFaultHi:stFaultLo
  6681. bra L1463 ; Branch to increase sensrChkIdx
  6682.  
  6683. ;---------------------------------------------------
  6684. ; Sensor check returned zero, no error, reset bit
  6685. ;---------------------------------------------------
  6686. L1462 ldd $00,x ; [a:b] = t_snsrChkBit(sensrChkIdx)
  6687. coma ; complement all bits
  6688. comb ; complement all bits
  6689. anda faultHi ; reset the bit if it was in faultHi
  6690. andb faultLo ; reset the bit if it was in faultLo
  6691. std faultHi ; Update faultHi:faultLo
  6692.  
  6693. ;------------------------------------------------------
  6694. ; Go to next sensor and re-init T2_snsrChk to 4 sec
  6695. ;------------------------------------------------------
  6696. L1463 inc sensrChkIdx ; Increment index (go to next sensor check subroutine next time)
  6697. ldaa #$08 ; 4 sec
  6698. staa T2_snsrChk ; re-init timer to 4 sec
  6699.  
  6700. ;-------------------------------------------------------------------------------
  6701. ; Section to verify the O2 sensor under specific conditions
  6702. ;-------------------------------------------------------------------------------
  6703. ;-------------------------------------------------------------------------------
  6704. ; First check if o2Raw indicate rich or lean, set a flag in b for now
  6705. ;-------------------------------------------------------------------------------
  6706. L1464 clrb ; assume we are running lean, b = 0
  6707. ldaa o2Raw ; a = o2Raw
  6708. cmpa #$1f ;
  6709. bcs L1465 ; Branch if o2Raw < 0.6V (lean)
  6710. orab #$80 ; Set flag indicating we are running rich
  6711.  
  6712. ;-------------------------------------------------------------------------------
  6713. ; Now check if all the conditions are met to do the verfication
  6714. ;
  6715. ; engine has been running for more than 180sec
  6716. ; baro sensor value is reliable
  6717. ; no fault code on baro, coil, iat, ect, cas
  6718. ; ectRaw and iatRaw are within acceptable range
  6719. ; temperature(ectRaw) > 86degC
  6720. ; temperature(iatRaw) > 0degC
  6721. ; temperature(iatRaw) < 55degC
  6722. ; engine is running normally (no fuel cut, etc..)
  6723. ; airVolTB < $68
  6724. ; airVolTB > $33
  6725. ; rpm31 < 4000
  6726. ; rpm31 > 2000
  6727. ; all conditions for closed loop mode are met
  6728. ;
  6729. ;-------------------------------------------------------------------------------
  6730. L1465 ldaa T0p5_crank2 ; a = T0p5_crank2
  6731. adda #$5a ;
  6732. bcs L1467 ; Bail if its been less than 180sec since engine started rotating
  6733. ldaa T40_baro ; T40_baro
  6734. bne L1467 ; Bail if T40_baro not zero (meaning baro sensor value is not reliable)
  6735. brset faultLo, #$42, L1467 ; Bail if errors on baro or coil
  6736. brset faultHi, #$26, L1467 ; Bail if errors on iat, ect or cas
  6737. brset state2, #$03, L1467 ; Bail ifectRaw or iatRaw out of acceptable range
  6738. ldaa ectRaw ; a = ectRaw
  6739. cmpa #$1c ;
  6740. bcc L1467 ; Bail if temperature(ectRaw) <= 86degC
  6741. ldaa iatRaw ; a = iatRaw
  6742. cmpa #$b3 ;
  6743. bcc L1467 ; Bail if temperature(iatRaw) <= 0degC
  6744. cmpa #$41 ;
  6745. bls L1467 ; Bail if temperature(iatRaw) >= 55degC
  6746. brset state1, #$1f, L1467 ; Bail if engine not running normally (i.e. notRotating or startingToCrank or rotatingStopInj or runningFast or no pulse accumulator interrupts received )
  6747. ldaa airVolTB ; a = airVolTB
  6748. #ifdef E931
  6749. cmpa #$68 ;
  6750. bcc L1467 ; Bail if airVolTB >= $68
  6751. cmpa #$33 ;
  6752. #else
  6753. cmpa #$90 ;
  6754. bcc L1467 ;
  6755. cmpa #$1a ;
  6756. #endif
  6757. bls L1467 ; Bail if airVolTB <= $33
  6758. ldaa rpm31 ; a = rpm31
  6759. cmpa #$80 ;
  6760. bcc L1467 ; Bail if rpm31 >= 4000
  6761. cmpa #$40 ;
  6762. bls L1467 ; Bail if rpm31 <= 2000
  6763. brclr closedLpFlags, #$02, L1467 ; Bail if not all conditions for closed loop mode are met
  6764.  
  6765. ;---------------------------------------------------------
  6766. ; All the conditions are met
  6767. ; At this point, b = $80 if o2Raw is rich, $00 otherwise
  6768. ;---------------------------------------------------------
  6769. tba ; a = $80 if o2Raw rich else $00
  6770. adda validFlags ; a = validFlags + ($80 or $00)
  6771. bmi L1466 ; Branch if running rich (validFlags + ($80 or $00) > $80 only if o2Raw>=0.6V above...)
  6772.  
  6773. ;-------------------------------
  6774. ; Running lean (o2Raw < 0.6V)
  6775. ;-------------------------------
  6776. ldaa T2_o2Chk ; a = T2_o2Chk
  6777. bne L1468 ; Branch if timer not expired, its been less than 30sec since all conditions were met and we are running lean
  6778. brset validFlags, #$02, L1468 ; Branch if o2 sensor condition already determined (no need to check further)
  6779.  
  6780. ;------------------------------------------------------
  6781. ; Timer is expired and the o2 sensor condition is not
  6782. ; already determined. Since we have been running lean
  6783. ; for over 30sec, we know the sensor is bad...
  6784. ;
  6785. ; Set the flag indicating the o2 sensor conditions was
  6786. ; determined, and increment o2BadCnt (with max of 255) to
  6787. ; indicate we have an error condition
  6788. ; Note that o2BadCnt increase by 1 max every time
  6789. ; the ECU is turned on (which resets validFlags.1)
  6790. ;------------------------------------------------------
  6791. orm validFlags, #$02 ; Set flag indicating o2 sensor condition was checked (bad in this case)
  6792. inc o2BadCnt ; o2BadCnt = o2BadCnt + 1
  6793. bne L1468 ; Branch to continue if o2BadCnt != 0
  6794. dec o2BadCnt ; o2BadCnt equals 0, go back to 255
  6795. bra L1468 ; branch to continue
  6796.  
  6797. ;-----------------------------------------------------------------
  6798. ; Running rich (o2Raw >= 0.6V)
  6799. ; As soon as we are running rich we know the sensor
  6800. ; is good, clear fault and set flag indicating o2 sensor
  6801. ; conditions was determined (good in this case)
  6802. ;-----------------------------------------------------------------
  6803. L1466 clr o2BadCnt ; o2BadCnt = 0
  6804. orm validFlags, #$02 ; Set flag indicating o2 sensor condition was checked (ok in this case)
  6805.  
  6806. ;-------------------------------
  6807. ; Re-init timer T2_o2Chk to 30sec
  6808. ;-------------------------------
  6809. L1467 ldaa #$3c ; 30sec
  6810. staa T2_o2Chk ;
  6811.  
  6812. ;------------------------------------------------------------
  6813. ; Update validFlags.7 rich/lean flag with current o2 conditions
  6814. ;------------------------------------------------------------
  6815. L1468 andm validFlags, #$7f ; Reset rich/lean flag
  6816. addb validFlags ; Add current rich/lean flag (set to 1 if rich)
  6817. stab validFlags ; Update validFlags
  6818.  
  6819. ;---------------------------------------------------------------------------
  6820. ; if o2BadCnt >= 1, set o2 fault code in current and stored fault variables
  6821. ;---------------------------------------------------------------------------
  6822. ldaa o2BadCnt ;
  6823. cmpa #$01 ;
  6824. bcs L1469 ; Branch to no o2 fault if o2BadCnt=0
  6825. orm faultHi, #$01 ; set oxygen sensor fault code?
  6826. orm stFaultHi, #$01 ;
  6827. bra L1470 ;
  6828.  
  6829. ;------------------------------------------------------------
  6830. ; Reset o2 fault code in only current fault variables
  6831. ;------------------------------------------------------------
  6832. L1469 andm faultHi, #$fe ; Clear oxygen sensor fault code
  6833.  
  6834. ;-------------------------------------------------------------------------------
  6835. ; Check if all the conditions are met to test the egrt sensor validity
  6836. ;
  6837. ; more than ($5a or $b4)/0.5 sec have elapsed since engine started rotating
  6838. ; baro sensor value is reliable
  6839. ; no fault code on baro, coil, iat, ect, cas
  6840. ; ectRaw and iatRaw are within acceptable range
  6841. ; temperature(ectRaw) > 86degC
  6842. ; temperature(iatRaw) < 55degC
  6843. ; baroRaw > 0.92bar
  6844. ; rpm31 < 3500
  6845. ; rpm31 > 2094
  6846. ; airVol < L2048(rpm)
  6847. ; airVol > L2047(rpm)
  6848. ;
  6849. ;-------------------------------------------------------------------------------
  6850. L1470 ldaa egrtTimerThr ; a = egrtTimerThr
  6851. adda T0p5_crank2 ;
  6852. bcs L1474 ; Bail if less than ($5a or $b4)/0.5 sec have elapsed since engine started rotating
  6853. ldaa T40_baro ;
  6854. bne L1474 ; Bail if T40_baro not zero (meaning baro sensor value is not reliable)
  6855. brset faultLo, #$42, L1474 ; Bail if errors on baro or coil
  6856. brset faultHi, #$26, L1474 ; Bail if errors on iat, ect or cas
  6857. brset state2, #$03, L1474 ; Bail ifectRaw or iatRaw out of acceptable range
  6858. ldaa ectRaw ;
  6859. cmpa #$1c ;
  6860. bcc L1474 ; Bail if temperature(ectRaw) <= 86degC
  6861. ldaa iatRaw ;
  6862. cmpa #$42 ;
  6863. bls L1474 ; Bail if temperature(iatRaw) >= 55degC
  6864. ldaa baroRaw ;
  6865. cmpa #$bd ;
  6866. bcs L1474 ; Bail if baroRaw < 0.92bar
  6867. ldaa rpm31 ;
  6868. cmpa #$70 ;
  6869. bcc L1474 ; Bail if rpm >=3500
  6870. cmpa #$43 ;
  6871. bls L1474 ; Bail if rpm <= 2094
  6872. ldx #L2048 ;
  6873. jsr interp16rpm ; b = L2048(rpm)
  6874. cmpb airVol ;
  6875. bls L1474 ; Bail if airVol >= L2048(rpm)
  6876. ldx #L2047 ;
  6877. jsr interp16rpm ; b = L2047(rpm)
  6878. cmpb airVol ;
  6879. bcc L1474 ; Bail if airVol <= L2047(rpm)
  6880.  
  6881. ;------------------------------------------
  6882. ; All basic condition are met,
  6883. ; bail if T2_egrtChk > 10
  6884. ;------------------------------------------
  6885. ldaa T2_egrtChk ;
  6886. cmpa #$0a ;
  6887. bhi L1478 ; Bail if T2_egrtChk > 10 ????
  6888.  
  6889. ;----------------------------------------------------
  6890. ; All basic condition are met and T2_egrtChk <= 10
  6891. ; Check the temperature indicated by the egrt
  6892. ; sensor and branch accordingly
  6893. ;----------------------------------------------------
  6894. ldaa #$05 ;
  6895. cmpa egrtRaw ;
  6896. bhi L1477 ; Branch if temperature(egrtRaw) > 307degCC (error, too hot)
  6897. ldx #L2046 ;
  6898. jsr iatCInterp ; b = L2046(iat)
  6899. cmpb egrtRaw ;
  6900. bcs L1477 ; Branch if temperature(egrtRaw) < L2046(iat) (error, too cold)
  6901.  
  6902. ;--------------------------------------------------------
  6903. ; At this point we know the egrt sensor is good
  6904. ; clear fault and set flag indicating sensor condition
  6905. ; was determined (good in this case)
  6906. ;--------------------------------------------------------
  6907. clr egrtBadCnt ; egrtBadCnt = 0, no fault
  6908. orm validFlags, #$04 ; Set flag indicating sensor condition was checked (ok in this case)
  6909. clra ; a = 0
  6910. bra L1475 ; Branch to set timer to 5 sec such that we continously check the sensor
  6911.  
  6912. ;----------------------------------------
  6913. ; Reset T2_egrtChk timer to 20 sec and bail
  6914. ;----------------------------------------
  6915. L1474 ldaa #$1e ; a = 15sec
  6916. L1475 adda #$0a ; a = a + 5sec
  6917. bcc L1476 ; Branch if no overflow
  6918. ldaa #$ff ; Use max of $ff
  6919. L1476 staa T2_egrtChk ; Update T2_egrtChk
  6920. bra L1478 ; Bail
  6921.  
  6922. ;----------------------------------------------------------------------------------
  6923. ; At this point we found that the sensor temperature is out of range (bad sensor)
  6924. ;----------------------------------------------------------------------------------
  6925. L1477 ldaa T2_egrtChk ; a = T2_egrtChk
  6926. bne L1478 ; Branch if timer not expired
  6927. brset validFlags, #$04, L1478 ; Branch if egrt sensor condition already determined
  6928.  
  6929. ;--------------------------------------------------------------------------
  6930. ; Sensor is bad, timer is expired and sensor condition not yet determined
  6931. ;
  6932. ; Set flag indicating sensor condition was determined (bad in this case)
  6933. ; and increment egrtBadCnt (255 max) to indicate we have an error condition
  6934. ; Note that egrtBadCnt increase by 1 max every time the ECU is turned off/on
  6935. ; (which resets validFlags.1)
  6936. ;--------------------------------------------------------------------------
  6937. orm validFlags, #$04 ; Set flag
  6938. inc egrtBadCnt ; egrtBadCnt = egrtBadCnt + 1
  6939. bne L1478 ; Bail if egrtBadCnt != 0
  6940. dec egrtBadCnt ; egrtBadCnt equals 0, go back to 255
  6941.  
  6942. ;---------------------------------------------------------------------------
  6943. ; if egrtBadCnt >= 2, set egrt fault code in current and stored fault variables
  6944. ;
  6945. ; egrtBadCnt >= 2 only if the ECU is turned off and then on again
  6946. ;---------------------------------------------------------------------------
  6947. L1478 ldaa egrtBadCnt ;
  6948. cmpa #$02 ;
  6949. bcs L1479 ; Branch if egrtBadCnt < 2
  6950. orm faultLo, #$20 ; Set egrt sensor fault flag
  6951. orm stFaultLo, #$20 ; Set egrt sensor fault flag
  6952. bra L1480 ;
  6953.  
  6954. ;------------------------------------------------
  6955. ; Reset egrt fault code in current fault variable
  6956. ;------------------------------------------------
  6957. L1479 andm faultLo, #$df ;
  6958.  
  6959. ;---------------------------------------------------------------------------
  6960. ; Reset egrt errors and fault codes if vehicle is not for California
  6961. ;---------------------------------------------------------------------------
  6962. L1480 brset config1, #$04, L1481 ; Bail if California car
  6963. clr egrtBadCnt ; reset egrt error count
  6964. andm stFaultLo, #$df ; Reset egrt stored fault code
  6965. andm faultLo, #$df ; Reset egrt current fault code
  6966.  
  6967. ;---------------------------------------------------------------------------
  6968. ; Reset N/A stored fault codes (fault codes don't correspond to anything)
  6969. ;---------------------------------------------------------------------------
  6970. L1481 andm stFaultHi, #$ef ;
  6971. andm stFaultLo, #$7f ;
  6972.  
  6973. ;----------------------------------------------------------------
  6974. ; If ECU is not about to turn-off check if there are faults set
  6975. ; and inital 5 sec "check engine light on" delay
  6976. ;----------------------------------------------------------------
  6977. ldaa T40_noPower ;
  6978. beq L1482 ; Branch to clear check engine light if timer expired (ECU is about to turn-off)
  6979. ldd faultHi ; d = faultHi:faultLo (current faults)
  6980. anda #$ef ; Reset N/A sensor fault bit
  6981. bne L1483 ; Branch to set check engine light if any errors left in faultHi
  6982. andb #$7e ; Reset vss and baro sensor in faultLo
  6983. bne L1483 ; Branch to set check engine light if any errors left in faultLo
  6984. ldaa T2_EcuPower ;
  6985. adda #$0a ;
  6986. bcs L1483 ; Branch if its been less than 5 sec since ECU power has been on
  6987.  
  6988. ;------------------------------------------------------------------
  6989. ; Its been more than 5 sec since ECU power has been on
  6990. ; and there is no fault set in faultHi:faultLo (apart from vss)
  6991. ; and ECU in not about to turn off
  6992. ;
  6993. ; Clear check engine light
  6994. ;------------------------------------------------------------------
  6995. L1482 orm port6, #$08 ; clear CE light
  6996. bra L1484 ;
  6997.  
  6998. ;-------------------------------------------------------------
  6999. ; Its been less than 5 sec since ECU power has been on
  7000. ; or there are faults set in faultHi:faultLo (apart from vss)
  7001. ; or ECU is about to turn off
  7002. ;
  7003. ; Activate check engine light
  7004. ;-------------------------------------------------------------
  7005. L1483 andm port6, #$f7 ; activate CE light
  7006.  
  7007.  
  7008. ;-----------------------------------------------------------------
  7009. ; Section to process diagnostic connector port commands/requests
  7010. ;-----------------------------------------------------------------
  7011. ;--------------------------------------------------------------
  7012. ; Bail to "heart beat" mode section if port rx is not enabled
  7013. ;--------------------------------------------------------------
  7014. L1484 brset sci_scr, #$08, L1485 ; Branch if serial port rx enabled.
  7015. jmp L_heartBeat ; rx not enabled, branch to section processing heart beat code
  7016.  
  7017. ;-----------------------------------------------------------
  7018. ; rx is enabled, we are in test mode. Check if anything
  7019. ; is being transmited or if anything new was received
  7020. ;-----------------------------------------------------------
  7021. L1485 brclr sci_scr, #$20, L1486 ; Branch if transmit data register is not empty (1 = empty...)
  7022. brset obdFlags, #$40, L1487 ; tx empty, branch if anything to process (e.g. a code was received (in interupt) and stored in obdCode)?
  7023.  
  7024. ;-------------------------------------------------
  7025. ; At this point rx enabled but tx not empty
  7026. ; or it is empty but there is nothing to process
  7027. ;
  7028. ; Reset action related registers and bail
  7029. ;-------------------------------------------------
  7030. L1486 orm obdInjCmd, #$3f ; reset any injector action
  7031. clr obdActCmd ; reset any actuator action
  7032. clr T40_obdCmd ; Clear action timer
  7033. jmp L1504 ; Bail to rest of code
  7034.  
  7035. ;------------------------------------------------------------
  7036. ; At this point we are in test mode, transmit register is
  7037. ; empty and a new code was received and stored in obdCode
  7038. ; Process the new code,
  7039. ;------------------------------------------------------------
  7040. ;------------------------------------------
  7041. ; Check if code is $fd (serial link test)
  7042. ;------------------------------------------
  7043. #ifdef E931
  7044. L1487 ldaa #$b5 ; Default value returned if code = $fd. (serial link test)
  7045. #else
  7046. L1487 ldaa #$b7 ; Default value returned if code = $fd. (serial link test)
  7047. #endif
  7048. ldab obdCode ; load received OBD code
  7049. cmpb #$fd ;
  7050. bcs L1490 ; branch if code is lower than $FD
  7051. beq L1496 ; bail to send response if code is equal to $fd
  7052.  
  7053. ;--------------------------------------------------------
  7054. ; obdCode equals $fe or $ff, respond with high or low
  7055. ; part ($fe or $ff code) of configration data (t_strap3)
  7056. ;--------------------------------------------------------
  7057. ldx #t_strap3 ; x = configuration data
  7058. tba ;
  7059. jsr cfgLookup16 ;
  7060. cmpa #$fe ; compare to FE
  7061. xgdx ;
  7062. beq L1489 ;
  7063. tba ;
  7064. L1489 bra L1496 ; branch to send on serial
  7065.  
  7066. ;------------------------
  7067. ; obdCode lower than $fd
  7068. ;------------------------
  7069. L1490 cmpb #$f1 ;
  7070. bcc L1494 ; branch if code is larger or equal to $f1
  7071. cmpb #$40 ;
  7072. bcs L1491 ; branch if code is lower than $40
  7073.  
  7074. ;------------------------
  7075. ; $40 <= obdCode < $f1
  7076. ; Check if it is $ca
  7077. ;------------------------
  7078. cmpb #$ca ;
  7079. beq L1493 ; branch if code equals $ca
  7080.  
  7081. ;----------------------------------------------------------------------
  7082. ; $40 <= obdCode < $f1 and it is not $ca
  7083. ; just respond with whatever is stored in the corresponding memory
  7084. ;----------------------------------------------------------------------
  7085. clra ; a=0
  7086. xgdx ; x = obdCode
  7087. ldaa $00,x ; a = whatever is in corresponding memory
  7088. bra L1496 ; branch to send on serial
  7089.  
  7090. ;----------------------------------------------------------------------
  7091. ; obdCode lower than $40, respond with sensor value
  7092. ; ($3e and $3f are converted to $3d)
  7093. ;----------------------------------------------------------------------
  7094. L1491 ldx #obdTable ; x points to obdTable
  7095. cmpb #$3d ;
  7096. bls L1492 ; branch if obdCode <= $3d
  7097. ldab #$3d ; Use max of $3d ($3e and $3f are converted to $3d???)
  7098. L1492 abx ; x points to "sensor" address in table
  7099. ldab $00,x ; b = obdTable(obdCode)
  7100. clra ; a = 0
  7101. xgdx ; x = d = obdTable(obdCode)
  7102. ldaa $00,x ; a = sensor value
  7103. bra L1496 ; branch to send on serial
  7104.  
  7105. ;----------------------------------------------------
  7106. ; obdCode = $ca, clear error faults if engine is not
  7107. ; rotating and respond with $00
  7108. ;----------------------------------------------------
  7109. L1493 ldaa T40_engRot ;
  7110. bne L1502 ; Don't reset fault codes if engine is rotating, we use them to run the car...
  7111. clra ;
  7112. clrb ;
  7113. std stFaultHi ; Erase fault codes
  7114. std faultHi ; Erase fault codes
  7115. staa o2BadCnt ; Erase o2 sensor error count
  7116. staa egrtBadCnt ; Erase egrt sensor error count
  7117. clra ;
  7118. bra L1496 ; Branch to send $00 on serial
  7119.  
  7120. ;----------------------------------------------------
  7121. ; obdCode >= $f1, this is a command/action code
  7122. ; Check if any action is already ongoing
  7123. ;----------------------------------------------------
  7124. L1494 ldaa obdInjCmd ; a = obdInjCmd
  7125. coma ; a = ~obdInjCmd
  7126. anda #$3f ; a = ~obdInjCmd & 00111111
  7127. bne L1495 ; branch if any injector already turned-off
  7128. brclr obdActCmd, #$ff, L1497 ; branch to continue processing if no actuator previously activated
  7129.  
  7130. ;----------------------------------------------------
  7131. ; obdCode >= $f1 and an action is already ongoing
  7132. ;----------------------------------------------------
  7133. L1495 ldaa T40_obdCmd ;
  7134. bne L1497 ; Branch if timer not expired
  7135.  
  7136. ;----------------------------------------------------
  7137. ; Action already ongoing and timer is expired,
  7138. ; reset all injector and actuators to normal mode and
  7139. ; respond with $00 (ignore new action)
  7140. ;----------------------------------------------------
  7141. orm obdInjCmd, #$3f ; set all injectors to normal operation
  7142. clr obdActCmd ; clear current actuator commands
  7143. ldaa #$00 ;
  7144. L1496 jmp L1503 ;
  7145.  
  7146. ;-----------------------------------------------
  7147. ; No action is ongoing or an action is ongoing
  7148. ; but not finished (timer not expired)
  7149. ;
  7150. ; continue processing new code
  7151. ;-----------------------------------------------
  7152. L1497 cmpb #$f6 ;
  7153. bls L1498 ; branch if code is $f1 to $f6
  7154.  
  7155. ;-----------------------------------------------------------
  7156. ; $f7 <= obdCode <= $fc, this is a turn injector off command
  7157. ; Bail if vehicle is moving
  7158. ;-----------------------------------------------------------
  7159. ldaa vssCnt1 ; a = check speed
  7160. beq L1499 ; Branch if speed is close to 0
  7161. bra L1502 ; speed too high, bail (safety I assume)
  7162.  
  7163. ;----------------------------------------------------
  7164. ; $f1 <= obdCode <= f6, this is an actuator command
  7165. ; Bail if engine is rotating
  7166. ;----------------------------------------------------
  7167. L1498 ldaa T40_engRot ; code is F1 to F6, check if "engine running"?
  7168. bne L1502 ; bail if engine is rotating (safety I assume)
  7169.  
  7170. ;------------------------------------------------------------
  7171. ; $f1<= obdCode <= $f6 and it is safe to perform the action
  7172. ; continue processing command/action code
  7173. ;------------------------------------------------------------
  7174. ;----------------------------------
  7175. ; Bail if any injector is already
  7176. ; turned-off by previous command
  7177. ;----------------------------------
  7178. L1499 ldaa obdInjCmd ; a = obdInjCmd
  7179. coma ; a = ~obdInjCmd
  7180. anda #$3f ; a = ~obdInjCmd & 00111111
  7181. bne L1504 ; bail if any injector already off
  7182.  
  7183. ;----------------------------------
  7184. ; Bail if an actuator is already
  7185. ; turned-on by previous command
  7186. ;----------------------------------
  7187. brset obdActCmd, #$ff, L1504 ; bail if a previous action is ongoing
  7188.  
  7189. ;-----------------------------------------
  7190. ; Check if injector or actuator command
  7191. ;-----------------------------------------
  7192. subb #$f7 ; b = obdCode - $f7
  7193. bcs L1500 ; Branch if obdCode < $f7 (actuator command)
  7194.  
  7195. ;-------------------------------------------
  7196. ; obdCode >=$f7, it is an injector command,
  7197. ; Ignore it for injectors 5 and 6
  7198. ;-------------------------------------------
  7199. subb #$02 ; b = injIndex = obdCode - $f7 - $02 = -2 to 3 range (injector 6 to 1 respectively...)
  7200. bcs L1502 ; branch if code is negative (injectors 5 and 6, do nothing)
  7201.  
  7202. ;----------------------------------
  7203. ; obdCode >=$f7, injector command
  7204. ;----------------------------------
  7205. ldx #t_obdInjMask ; x points to t_obdInjMask (table t_obdInjMask: $fb $fd $f7 $fe)
  7206. abx ;
  7207. ldaa $00,x ; a = t_obdInjMask(injIndex) ($fb $fd $f7 $fe for injector 4 3 2 1 resp.-> order in nibble -> 2 4 3 1)
  7208. clrb ; b = $00
  7209. bra L1501 ;
  7210.  
  7211. ;--------------------------------------------
  7212. ; obdCode <$f7, it is an actuator command
  7213. ;--------------------------------------------
  7214. L1500 addb #$06 ; b = actIndex = obdCode - $f7 + $06 = 0 to 5
  7215. ldx #t_obdActMask ; x points to t_obdActMask: 20 10 08 04 01 02
  7216. abx ;
  7217. ldab $00,x ; b = t_obdActMask(actIndex)
  7218. ldaa #$ff ; a = $ff
  7219.  
  7220. ;-----------------------------------------------------------
  7221. ; At this point,
  7222. ; a contains the new injector to turn off, if any
  7223. ; b contains the new actuator to turn on, if any
  7224. ;
  7225. ; Update obdInjCmd and obdActCmd. Notice that only one
  7226. ; actuator is activated at a time but multiple injectors
  7227. ; can be turned off...
  7228. ;-----------------------------------------------------------
  7229. L1501 sei ; Disable interrupts
  7230. anda obdInjCmd ; Turn off the new injector and continue turning off the existing ones
  7231. staa obdInjCmd ; Update obdInjCmd
  7232. stab obdActCmd ; Turn on the new actuator
  7233. cli ;
  7234.  
  7235. ;--------------------------------------------
  7236. ; Re-init T40_obdCmd timer to 6sec and bail
  7237. ;--------------------------------------------
  7238. ldaa #$f0 ; 6 sec
  7239. staa T40_obdCmd ; T40_obdCmd = 6 sec
  7240. bra L1504 ; jump to RTS
  7241.  
  7242. ;--------------------------------------------
  7243. ; Reset any ongoing actions (injector or actuator)
  7244. ;--------------------------------------------
  7245. L1502 orm obdInjCmd, #$3f ; Set all injectors to on
  7246. clr obdActCmd ; Set all actuators to off
  7247. clr T40_obdCmd ; Clear timer
  7248. ldaa #$ff ; respond with $ff
  7249.  
  7250. ;-----------------------------------------------------------------
  7251. ; At this point a contains the response to send on diagnostic port
  7252. ; send it...
  7253. ;-----------------------------------------------------------------
  7254. L1503 staa sci_tx ; either FF or 00 or output value to serial port
  7255. orm obdFlags, #$80 ; set bit indicating something has been sent
  7256. andm obdFlags, #$bf ; reset bit $40 since we finished processing the request
  7257.  
  7258. ;---------------------------------------------
  7259. ; Jump to rest of code (skip heart beat mode)
  7260. ;---------------------------------------------
  7261. L1504 jmp L1524 ; Jump to RTS
  7262.  
  7263.  
  7264.  
  7265. ;****************************************************************
  7266. ;
  7267. ; Used in processing the output of error codes to test connector
  7268. ;
  7269. ; Used to Represent the number of shift we need to apply...
  7270. ;
  7271. ;****************************************************************
  7272. t_errCodeShift .byte $80, $40, $20, $10, $08, $04, $02, $01
  7273.  
  7274.  
  7275.  
  7276. ;****************************************************************
  7277. ;
  7278. ; Output error codes to test connector (heart beat mode)
  7279. ;
  7280. ; a and b are used throughout this code to contain
  7281. ; the old/new values of errCodeProc and errCodeIdx
  7282. ;
  7283. ; Freakin difficult code to disassemble!
  7284. ;
  7285. ;
  7286. ;****************************************************************
  7287. ;----------------------------
  7288. ; Only execute code at 2Hz
  7289. ;----------------------------
  7290. L_heartBeat brset Tclocks, #$04, L1507 ; Branch if 2Hz signal set
  7291. jmp L1524 ; 2Hz signal not yet set, bail
  7292.  
  7293. ;--------------------------------------------------
  7294. ; Load basic variables and check whether
  7295. ; a code is currently being output to connector
  7296. ;--------------------------------------------------
  7297. L1507 ldaa errCodeProc ; a = errCodeProc
  7298. ldab errCodeIdx ; b = errCodeIdx
  7299. tsta ;
  7300. bne L1508 ; Branch if errCodeProc != 0 ( a code is currently being output)
  7301. bitb #$e0 ; test 3 bit 2Hz timer...
  7302. beq L1509 ; Branch if timer is expired (we are really finished with previous code...)
  7303. L1508 jmp L1520 ; Jump to continue processing the code currently being output
  7304.  
  7305. ;---------------------------------------------------------------
  7306. ; At this point we are not processing anything, continue trying
  7307. ;---------------------------------------------------------------
  7308. L1509 clra ; a = 0
  7309. staa temp1 ; temp1 = 0
  7310. L1510 ldaa stFaultHi ; preload a = stFaultHi
  7311. stab temp2 ; temp1:temp2 = errCodeIdx, notice "stab" changes zero flag for branch below...
  7312. beq L1512 ; Branch if errCodeIdx = 0
  7313.  
  7314. ;---------------------------------------------
  7315. ; errCodeIdx > 0
  7316. ; Check if errCodeIdx <= 8
  7317. ;---------------------------------------------
  7318. ldx #t_errCodeShift-1 ; x points to t_errCodeShift-1
  7319. subb #$08 ; b = errCodeIdx - 8
  7320. bls L1511 ; Branch if errCodeIdx <= 8
  7321.  
  7322. ;----------------------------------------------
  7323. ; errCodeIdx > 8, current index is in low fault
  7324. ;----------------------------------------------
  7325. ldaa stFaultLo ; a = stFaultLo since thats what we should be using
  7326. abx ; x points to t_errCodeShift - 1 + (errCodeIdx - 8)
  7327. ldab $00,x ; b = t_errCodeShift(errCodeIdx)
  7328. mul ; shift whats left to process in high part of d (in a)
  7329. ldx temp1 ; x = 0:errCodeIdx
  7330. tsta ; test if any fault bit set
  7331. bra L1513 ; Branch
  7332.  
  7333. ;-----------------------------------------------
  7334. ; errCodeIdx <= 8, current index is in high fault
  7335. ;-----------------------------------------------
  7336. L1511 addb #$08 ; b = errCodeIdx - 8 + 8 = errCodeIdx
  7337. abx ; x points to t_errCodeShift-1 + errCodeIdx
  7338. ldab $00,x ; b = t_errCodeShift(errCodeIdx)
  7339. mul ; shift whats left to process in high part of d (in a)
  7340.  
  7341. L1512 ldx temp1 ; x = 0:errCodeIdx
  7342. tsta ; test if any fault bit set
  7343. bne L1514 ; Branch if any fault bit were set
  7344.  
  7345. ;-----------------------------------------------
  7346. ; No fault bit set in high part, try low part now
  7347. ;-----------------------------------------------
  7348. ldx #$0008 ; new errCodeIdx x = 8
  7349. ldaa stFaultLo ; a = stFaultLo
  7350.  
  7351. ;--------------------------------------------------
  7352. ; At this point, a contains the error bits left (if any)
  7353. ; to process and x is the current index error
  7354. ;--------------------------------------------------
  7355. L1513 beq L1515 ; branch if no error are set in what is left to process
  7356.  
  7357. ;---------------------------------
  7358. ; Loop until we find the bit
  7359. ;---------------------------------
  7360. L1514 inx ; ++x
  7361. lsra ; Shift lowest bit in carry
  7362. bcc L1514 ; Loop if bit was 0
  7363.  
  7364. ;----------------------------------------------------------------
  7365. ; We found a bit set -> we found the next error code to output...
  7366. ; update new errCodeProc value (in a for now) and new errCodeIdx
  7367. ; (in a for now)
  7368. ;----------------------------------------------------------------
  7369. stx temp1 ; temp1:temp2 = newBitIndex of next code????
  7370. ldab temp2 ; b = newBitIndex
  7371. ldx #t_snsrChkCode-1 ; x points to t_snsrChkCode-1
  7372. abx ; x points to t_snsrChkCode-1 + newBitIndex
  7373. ldaa $00,x ; a = t_snsrChkCode(newBitIndex)
  7374. oraa #$80 ; a = t_snsrChkCode(newBitIndex) | $80
  7375. andb #$1f ; b = newBitIndex & $1f
  7376. orab #$a0 ; Set newBitIndex high bits (timer) to 2.5 sec
  7377. bra L1521 ; Branch to set heart beat output and store a and b
  7378.  
  7379. ;------------------------------------------------------
  7380. ; No error found in stFaultLo, this means we are
  7381. ; at the end of the cycle... (we checked high part
  7382. ; first and then low part), restart the whole
  7383. ; thing from errCodeIdx=0 if errCodeIdx not already at 0
  7384. ;------------------------------------------------------
  7385. L1515 clrb ; b = 0 = new value of errCodeIdx
  7386. ldaa temp2 ; a = old errCodeIdx
  7387. bne L1510 ; Loop back if old errCodeIdx != 0
  7388.  
  7389. ;--------------------------------------------------------------------------
  7390. ; errCodeIdx already at 0, there where no error during the cycle
  7391. ; toggle heart beat sent to diagnostic port, this is the "no fault" signal
  7392. ;--------------------------------------------------------------------------
  7393. psha ; st0 = a, why, we know a=0 from test above???????????
  7394. sei ; Make sure no interrupt plays with port2 while we change it
  7395. ldaa port2 ;
  7396. eora #$10 ; Toggle heart beat sent to diagnostic port?
  7397. staa port2 ; Update port
  7398. cli ;
  7399. pula ; a = st0 = 0
  7400. bra L1523 ; Branch to exit
  7401.  
  7402. ;----------------------------------------------------------------
  7403. ; errCodeIdx upper 3 bit timer is expired (Branch from below)
  7404. ; First check if there are more long pulse to ouptput
  7405. ;----------------------------------------------------------------
  7406. L1516 bita #$70 ;
  7407. beq L1518 ; Branch if no more long pulse code (errCodeProc & 01110000 = 0)
  7408.  
  7409. ;-------------------------------------
  7410. ; There are more long pulse to output
  7411. ;-------------------------------------
  7412. suba #$10 ; decrement the number of long pulse by 1
  7413. andb #$1f ; reset timer to 0 (upper 3 bits)
  7414. orab #$80 ; Set 2Hz 3 bit timer to 4 (2 sec)
  7415. L1517 andm port2, #$ef ; Set heart beat sent to diagnostic port
  7416. bra L1522 ; Branch to update timer and exit
  7417.  
  7418. ;----------------------------------------------------
  7419. ; No more long pulse to output
  7420. ; Check if bit 7 is set (was set when we
  7421. ; started output of this code)
  7422. ;----------------------------------------------------
  7423. L1518 bita #$80 ;
  7424. beq L1519 ; Branch if bit is not set
  7425.  
  7426. ;-----------------------------------------------------------------------------
  7427. ; bit 7 is set, we are therefore at the midpoint between long and short pulse
  7428. ;
  7429. ; reset the flag, set the timer to 1.5 sec, reset output and exit
  7430. ; This is basically a 1.5 sec pause in between long and short pulse
  7431. ;-----------------------------------------------------------------------------
  7432. anda #$7f ; Reset bit 7 (nothing left to output flag). At this point a should be equal to $7f ???
  7433. andb #$1f ; Reset timer to 0 (upper 3 bits)
  7434. orab #$60 ; Set 2Hz 3 bit timer to 3 (1.5 sec)
  7435. bra L1521 ; Branch to reset hearth beat, update timer and exit
  7436.  
  7437. ;-----------------------------------------------------------------
  7438. ; Flag was not set, just output whatever short pulses are left...
  7439. ;-----------------------------------------------------------------
  7440. L1519 deca ; decrement the number of short pulse by 1
  7441. andb #$1f ; reset timer to 0 (upper 3 bits)
  7442. orab #$40 ; Set 2Hz 3 bit timer to 2 (1 sec)
  7443. bra L1517 ; Branch to set hearth beat, update timer and exit
  7444.  
  7445. ;---------------------------------
  7446. ; A code is currently being output...
  7447. ; At this point we have
  7448. ; a = errCodeProc
  7449. ; b = errCodeIdx
  7450. ;---------------------------------
  7451. L1520 cmpb #$20 ;
  7452. bcs L1516 ; Branch if errCodeIdx < $20 (e.g. timer=0, upper 3 bit timer is expired)
  7453.  
  7454. ;-------------------------------------
  7455. ; Timer not expired check if timer>1
  7456. ;-------------------------------------
  7457. cmpb #$3f ;
  7458. bhi L1522 ; Branch if errCodeIdx > $3f (timer>1)
  7459.  
  7460. ;-------------------------------------------
  7461. ; timer = 1, set heart beat mode output
  7462. ;-------------------------------------------
  7463.  
  7464. ;-------------------------------------------
  7465. ; Reset heart beat output on diagnostic port
  7466. ;-------------------------------------------
  7467. L1521 orm port2, #$10 ; Reset heart beat sent to diagnostic port
  7468.  
  7469. ;-------------------------------------------------------------
  7470. ; Decrement errCodeIdx timer (upper 3 bits) by $20 (0.5sec)
  7471. ;-------------------------------------------------------------
  7472. L1522 subb #$20 ;
  7473.  
  7474. ;-----------------------------------------------------
  7475. ; Store new errCodeProc and errCodeIdx and return
  7476. ;-----------------------------------------------------
  7477. L1523 staa errCodeProc ;
  7478. stab errCodeIdx ;
  7479. L1524 rts ;
  7480.  
  7481.  
  7482.  
  7483. ;****************************************************************
  7484. ;
  7485. ;
  7486. ; Maf sensor check:
  7487. ;
  7488. ;
  7489. ;****************************************************************
  7490. test_maf ldaa rpm31 ;
  7491. cmpa #$10 ;
  7492. bls L1528 ;
  7493. ldaa T40_mas ;
  7494. beq L1526 ; Branch if timer expired (no mas interrupt for a long time...)
  7495. ldaa t2_diff8 ;
  7496. cmpa #$31 ; t2_diff8 more than 49 is an error?
  7497. bcs L1527 ;
  7498. L1526 decb ;
  7499. L1527 rts ;
  7500. L1528 incb ; return 1 (test not conclusive)
  7501. rts ;
  7502.  
  7503.  
  7504.  
  7505. ;******************************************************************
  7506. ;
  7507. ;
  7508. ; Crank angle sensor
  7509. ;
  7510. ;
  7511. ;******************************************************************
  7512. test_cas ldaa T40_engRot ;
  7513. bne L1530 ; Branch if sensor is fine. T40_engRot is non-zero when CAS interrupts are being received... sensor must be fine...
  7514. brset port3Snap0, #$40, L1531 ; Branch if key is not in start
  7515. decb ; Key is in start but T40_engRot is 0, should not happen -> CAS is bad
  7516. L1530 rts ;
  7517. L1531 incb ; return 1 (test not conclusive)
  7518. rts
  7519.  
  7520.  
  7521.  
  7522. ;******************************************************************
  7523. ;
  7524. ;
  7525. ; Top dead sensor
  7526. ;
  7527. ;
  7528. ;******************************************************************
  7529. test_tdc ldaa T40_engRot ;
  7530. beq L1535 ; Branch if timer is 0, (engine not rotating)
  7531. ldaa tdcCheck ; Engine rotating, check if #1 TDC signal is being received
  7532. beq L1533 ; Branch to error if #1 TDC signal is not being received
  7533. ldaa tdcCasCount ;
  7534. cmpa #$04 ;
  7535. bcc L1534 ; Branch if tdcCasCount>=4
  7536. brclr port3, #$40, L1535 ; tdcCasCount<4, branch if key is in start
  7537. L1533 decb ; Set error
  7538. L1534 rts ;
  7539. L1535 incb ; return 1 (test not conclusive)
  7540. rts ;
  7541.  
  7542.  
  7543.  
  7544. ;******************************************************************
  7545. ;
  7546. ;
  7547. ; Reed switch (VSS) sensor check:
  7548. ;
  7549. ;
  7550. ;******************************************************************
  7551. test_reed ldaa vssCnt1
  7552. bne L1537 ; branch if car is moving (no error since we detected that...)
  7553. brset state1, #$02, L1538 ; Branch if no pulse accumulator interrupts?
  7554. brset port3Snap0, #$80, L1538 ; Branch if Idle switch on (car most likely not moving...)
  7555. ldaa rpm31 ;
  7556. cmpa #$60 ;
  7557. bls L1538 ;
  7558. ldaa airVol ;
  7559. cmpa #$6d ;
  7560. bls L1538 ;
  7561. brset state3, #$04, L1538 ; Branch if rev limiter active
  7562. ldaa T2_stCrank ;
  7563. adda #$78 ;
  7564. bcs L1538 ;
  7565. decb ;
  7566. L1537 rts ;
  7567. L1538 incb ; return 1 (test not conclusive)
  7568. rts ;
  7569.  
  7570.  
  7571.  
  7572. ;******************************************************************
  7573. ;
  7574. ;
  7575. ; Injector circuit check
  7576. ;
  7577. ;
  7578. ;******************************************************************
  7579. test_inj ldaa obdInjCmd ; First check if we disabled an injector on purpose (OBD command)
  7580. coma ;
  7581. bita #$3f ; only keep 6 bits (6 inj.)
  7582. bne L1541 ; Branch if disabled on purpose
  7583. ldaa T40_engRot ;
  7584. beq L1541 ; Branch if engine not rotating
  7585. ldaa rpm31 ;
  7586. cmpa #$20 ;
  7587. bcc L1541 ; Branch if rpm > 1000
  7588. ldaa tpsRaw ;
  7589. cmpa #$24 ;
  7590. bcc L1541 ; Branch if tpsRaw > $24
  7591. brclr injBad, #$01, L1540 ; Branch if injector OK?
  7592. decb ; Error, set flag
  7593. L1540 rts ;
  7594. L1541 incb ; return 1 (test not conclusive)
  7595. rts
  7596.  
  7597.  
  7598.  
  7599. ;******************************************************************
  7600. ;
  7601. ;
  7602. ; Fuel pump relay check
  7603. ;
  7604. ;
  7605. ;******************************************************************
  7606. test_fpump brclr port3Snap0, #$40, L1543 ; Branch if key in start position
  7607. brset port1, #$10, L1545 ; Branch if fuel pump relay bit set
  7608. L1543 brclr port4Snap, #$40, L1544 ; Fuel pump driven feedback???
  7609. decb
  7610. L1544 rts
  7611. L1545 incb ; return 1 (test not conclusive)
  7612. rts
  7613.  
  7614.  
  7615.  
  7616. ;******************************************************************
  7617. ;
  7618. ;
  7619. ; Ignition coil check:
  7620. ;
  7621. ;
  7622. ;******************************************************************
  7623. test_coil brset state1, #$11, L1548 ; Branch if notRotating or startingToCrank
  7624. ldaa rpm31 ;
  7625. cmpa #$a0 ;
  7626. bcc L1548 ; Branch if RPM >= 5000
  7627. brclr coilChkFlags, #$80, L1547 ; Branch if no error found on ignition signal
  7628. decb ; Error was found
  7629. L1547 rts ;
  7630. L1548 incb ; return 1 (test not conclusive)
  7631. rts
  7632.  
  7633.  
  7634.  
  7635. ;******************************************************************
  7636. ;
  7637. ;
  7638. ; Knock sensor check:
  7639. ;
  7640. ;
  7641. ;******************************************************************
  7642. test_knock brset port4Snap, #$20, L1550 ; Knock sensor related???
  7643. decb
  7644. L1550 rts
  7645.  
  7646.  
  7647.  
  7648. ;******************************************************************
  7649. ;
  7650. ;
  7651. ; Intake air temperature sensor check:
  7652. ;
  7653. ;
  7654. ;******************************************************************
  7655. test_iat brclr state2, #$02, L1552
  7656. decb
  7657. L1552 rts
  7658.  
  7659.  
  7660.  
  7661. ;******************************************************************
  7662. ;
  7663. ;
  7664. ; Tps sensor check:
  7665. ;
  7666. ;
  7667. ;******************************************************************
  7668. test_tps ldaa tpsRaw
  7669. cmpa #$66
  7670. bhi L1554 ; branch if tpsRaw voltage higher 40%
  7671. cmpa #$0a ;
  7672. bcs L1555 ; branch if voltage lower than 4%
  7673. rts ;
  7674. L1554 brclr port3Snap0, #$80, L1556 ; branch if idle switch is off
  7675. L1555 decb ;
  7676. L1556 rts ;
  7677.  
  7678.  
  7679.  
  7680. ;******************************************************************
  7681. ;
  7682. ;
  7683. ; Ect sensor check:
  7684. ;
  7685. ;
  7686. ;******************************************************************
  7687. test_ect brset state2, #$10, L1563 ; Branch if flag was previously set when we were here (once in error always in error????)
  7688. brclr state2, #$01, L1558 ; Branch if no ect error flag set in main code
  7689. ldaa T2_stCrank ; ect error flag is set in main code, check timer
  7690. adda #$78 ;
  7691. bcc L1562 ; Branch if more than 60sec have elapsed since engine startedToCrank (should have cleared by now?, sensor is in error)
  7692. L1558 brset state1, #$10, L1560 ; Branch if notRotating (not conclusive)
  7693. ldaa T0p5_ect ;
  7694. beq L1563 ; Branch if temperature stayed at exactly 88degC for more than 5 minutes (????)
  7695. ldaa T0p5_crank2 ;
  7696. adda #$5a ;
  7697. bcs L1559 ; Branch if less than 180s have elapsed since ectStCrank was loaded
  7698.  
  7699. ;----------------------------------------------------------
  7700. ; More than 180s have elapsed since ectStCrank was loaded
  7701. ;----------------------------------------------------------
  7702. ldaa ectStCrank ;
  7703. cmpa #$ff ; Not sure how we would get that value, maybe if sensor is broken??
  7704. bcs L1559 ; Branch if ectStCrank < $ff (branch almost always???????)
  7705. suba #$0a ; a=ectStCrank-$0a = $ff-$0a = $f5, am I missing someting??????????????
  7706. cmpa ectFiltered ;
  7707. bcs L1562 ; Branch if ectFiltered > $f5 ?????? (sensor error)
  7708. L1559 ldaa ectFiltered ;
  7709. cmpa #$54 ; 41degC
  7710. bcs L1561 ; Branch if temperature(ectFiltered) > 41degC (no error)
  7711. L1560 incb ; return 1 (test not conclusive)
  7712. L1561 rts
  7713. L1562 ldaa T2_snsrChk
  7714. bne L1563
  7715. orm state2, #$10
  7716. L1563 decb
  7717. rts
  7718.  
  7719.  
  7720.  
  7721. ;******************************************************************
  7722. ;
  7723. ;
  7724. ; Barometer sensor check
  7725. ;
  7726. ;
  7727. ;******************************************************************
  7728. test_baro ldaa T40_baro ;
  7729. bne L1567 ;
  7730. ldaa baroRaw ;
  7731. cmpa #$e6 ;
  7732. bhi L1565 ;
  7733. cmpa #$0a ;
  7734. bcc L1566 ;
  7735. L1565 decb ;
  7736. L1566 rts ;
  7737. L1567 incb ; return 1 (test not conclusive)
  7738. rts
  7739.  
  7740.  
  7741.  
  7742. ;******************************************************************
  7743. ;
  7744. ;
  7745. ; Serial port interrupt subroutine
  7746. ;
  7747. ;
  7748. ;******************************************************************
  7749. serialRxInt ldd sci_scr ; A=sci_cr, B=sci_read
  7750. bita #$80 ; Check if something was received??/start of sequence to clear flag
  7751. beq L1570 ;
  7752. brset obdFlags, #$80, L1569 ; Branch if the code we received is the echo of the one we just sent
  7753. stab obdCode ;
  7754. orm obdFlags, #$40 ; Indicate new value available
  7755. bra L1570 ;
  7756. L1569 andm obdFlags, #$7f ; The code we received is the echo of what we sent, just drop it and reset flag
  7757. L1570 rti ;
  7758.  
  7759.  
  7760.  
  7761. ;******************************************************************
  7762. ;
  7763. ;
  7764. ;
  7765. ;
  7766. ;
  7767. ;******************************************************************
  7768. loadConfig ldx #t_strap1 ;
  7769. ldab port4 ;
  7770. nop ;
  7771. andb #$03 ; Keep only config resistor bits
  7772. aslb ;
  7773. abx ;
  7774. ldd $00,x ;
  7775. std config1 ;
  7776. rts ;
  7777.  
  7778.  
  7779.  
  7780. ;******************************************************************
  7781. ;
  7782. ;
  7783. ; Initialize a few things
  7784. ;
  7785. ;
  7786. ;******************************************************************
  7787. initFunc1 ldd #$7e16 ;
  7788. std p1_ddr ; Initialize port1 and port2 data direction registers
  7789. ldd #$0000 ;
  7790. std p3_ddr ; Initialize port3 and port4 data direction register (all inputs)
  7791. ldaa #$fe ;
  7792. staa p5_ddr ; Initialize port5 data direction register
  7793. clra ;
  7794. staa L000f ;
  7795. staa L0017 ;
  7796. andm port1, #$bf ; Reset ??????
  7797. ldaa #$00 ;
  7798. staa L0024 ;
  7799.  
  7800. ;-------------------------------------------------------
  7801. ; rti_freq is setting the real time interrupt frequency:
  7802. ; F=125000/(256-x)
  7803. ; x F
  7804. ; 0x64 1*801.28Hz
  7805. ; 0xb2 2*801.28Hz
  7806. ; 0xcc 3*801.28Hz
  7807. ; 0xd9 4*801.28Hz
  7808. ;-------------------------------------------------------
  7809. ldd #$4d64 ;
  7810. std rti_ctl
  7811. rts
  7812.  
  7813.  
  7814.  
  7815. ;******************************************************************
  7816. ;
  7817. ; Input: D = Tcas (period of cas interrupts in sec * 125000)
  7818. ; Output: freq = $EA600/D = 960000/D
  7819. ;
  7820. ; in the end, rpm = freq/8*31.25
  7821. ;
  7822. ; e.g. 1000RPM -> Tcas = 60/1000* 125000/2 = 3750
  7823. ; freq = 960000/3750 = 256
  7824. ; rpm = freq/8*31.25 = 1000 RPM
  7825. ;
  7826. ;******************************************************************
  7827. calcFreq std temp4 ;
  7828. ldd #$a600 ;
  7829. std temp2 ;
  7830. ldd #$000e ; numerator = D:temp2 = 000EA600
  7831. bra div3216 ; D = (#$000EA600)/([$5A $5B])
  7832.  
  7833.  
  7834.  
  7835. ;******************************************************************
  7836. ;
  7837. ;
  7838. ;
  7839. ;
  7840. ;******************************************************************
  7841. mul816_baro ldab baroFact
  7842.  
  7843. ;******************************************************************
  7844. ;
  7845. ;
  7846. ;
  7847. ;
  7848. ;******************************************************************
  7849. mul816_256 jsr mul816_128 ; D and [temp6:temp7] = b*[temp6:temp7]/128
  7850. jmp scale2m
  7851.  
  7852.  
  7853.  
  7854. ;******************************************************************
  7855. ;
  7856. ; Input
  7857. ; x = point to table1
  7858. ; y = point to table2
  7859. ;
  7860. ; Output
  7861. ; d = injMasComp * table1(rpm)/128 * table2(ect)/16 * baroFact/128
  7862. ;
  7863. ;******************************************************************
  7864. L1577 ldd injMasComp ; d = injMasComp
  7865. std temp6 ; temp6:temp7 = injMasComp
  7866. jsr interp16rpm ; b = table1(rpm)
  7867. jsr mul816_128 ; [temp6:temp7] = injMasComp * table1(rpm)/128
  7868. xgxy ; x points to table2
  7869. jsr interpEct ; b = table2(ect)
  7870. ldaa baroFact ; a = baroFact
  7871. mul ; d = table2(ect) * baroFact
  7872. jmp mul1616_2K ; d = 8* injMasComp * table1(rpm)/128 * table2(ect)/128 * baroFact/128
  7873.  
  7874.  
  7875.  
  7876. ;******************************************************************
  7877. ;
  7878. ; 8 bit by 16 bit multiplication
  7879. ;
  7880. ; Input
  7881. ; b: 8 bit value
  7882. ; [temp6:temp7]: 16 bit value, optionally in X if called from mul816b
  7883. ;
  7884. ; Output:
  7885. ; D and [temp1:temp2] = b*[temp6:temp7]/256
  7886. ; temp3 = Lo(b*temp7) (fractional part of result)
  7887. ;
  7888. ;
  7889. ;
  7890. ;******************************************************************
  7891. mul816 ldx temp6 ;
  7892. mul816b stx temp1 ; temp1,temp2 = temp6,temp7
  7893. pshb ; save b
  7894. ldaa temp2 ; a = temp7
  7895. mul ; d = b*temp7
  7896. std temp2 ; temp2, temp3 = b*temp7
  7897. ldaa temp1 ; a=temp6
  7898. pulb ;
  7899. mul ; d = b*temp6
  7900. addb temp2 ; d = b*temp6 + b*temp7/256 = b*(temp6+temp7/256) = b*[temp6:temp7]/256
  7901. adca #$00 ; Propagate addition carry
  7902. std temp1 ; temp1, temp2 = b*[temp6:temp7]/256
  7903. rts ;
  7904.  
  7905.  
  7906.  
  7907. ;******************************************************************
  7908. ;
  7909. ;
  7910. ; 16 bit by 16 bit multiplication:
  7911. ;
  7912. ; Input:
  7913. ; a:b = 16 bit value (val1:val2)
  7914. ; temp6:temp7 = 16 bit value1 (val3:val4)
  7915. ;
  7916. ; Output:
  7917. ; d = [a:b]*[temp6:temp7]/65536 (or equivalently, 16 upper bits of result)
  7918. ; temp2 = lower 8 bits (of 24) of results
  7919. ;----------------------------------------
  7920. ;
  7921. ; D * temp6:temp7
  7922. ; - 16 bit multiplication:
  7923. ; - D * temp6:temp7 , High bytes stored in D
  7924. ; -
  7925. ; - (1) * (2) is equal to ( 1A + 1B ) * ( 2A + 2B )
  7926. ;
  7927. ; - = 1A * 2A + 1A * 2B + 1B * 2A + 1B * 2B
  7928. ;
  7929. ; - Where:
  7930. ; - lets call temp6:temp7 Argument (1)
  7931. ; - lets call reg D Argument (2)
  7932. ; -
  7933. ; - temp6 is then named (1A)
  7934. ; - temp7 is then named (1B)
  7935. ; - and temp6:temp7 = (1A) * #$100 + (1B)
  7936. ; -
  7937. ; - Reg D is reg A:reg B
  7938. ; - A is then named (2A)
  7939. ; - B is then named (2B)
  7940. ; - temp6:temp7 = (1A) * #$100 + (1B)
  7941. ;
  7942. ; - D consists of (2A) * #$100 + (2B)
  7943. ;
  7944. ; -Result (R) will be 32 bit, (R3)-(R0) where
  7945. ; -temp3 = (R0) low
  7946. ; -temp2 = (R1)
  7947. ; -temp1 = (R2) --\ Stored and returned in D
  7948. ;
  7949. ;******************************************************************
  7950. mul1616 ldx #temp6 ; x points to temp6
  7951. psha ; st0 = val1
  7952. pshb ; st1 = val2
  7953. ldaa $01,x ; a = val4
  7954. mul ; d = val4*val2
  7955. std temp2 ; temp2:temp3=val4*val2
  7956. pulb ; b = val2
  7957. ldaa $00,x ; a = val3
  7958. mul ; d = val2*val3
  7959. addb temp2 ; d = val2*val3 + val4*val2/256
  7960. adca #$00 ; propagate carry
  7961. std temp1 ; temp1:temp2 = val2*val3 + val4*val2/256
  7962. pula ; a = val1
  7963. psha ; st0 = val1
  7964. ldab $01,x ; b = val4
  7965. mul ; d = val1*val4
  7966. addd temp1 ; d = val1*val4 + val2*val3 + val4*val2/256
  7967. std temp1 ; temp1:temp2 = val1*val4 + val2*val3 + val4*val2/256
  7968. pula ; a = val1
  7969. rorb ; shift in carry bit
  7970. andb #$80 ; keep only carry bit
  7971. pshb ; st0 = carry bit in position $80
  7972. ldab $00,x ; b=val3
  7973. mul ; d=val1*val3
  7974. xgdx ; x=val1*val3, d points to temp6
  7975. pulb ; b=carry bit in position $80
  7976. abx ; add 2 x carry bit (since shifted right previously)
  7977. abx ; x=val1*val3 + carry
  7978. ldab temp1 ; b = (val1*val4 + val2*val3 + val4*val2/256)/256
  7979. abx ; x = val1*val3 + carry + (val1*val4 + val2*val3 + val4*val2/256)/256
  7980. xgdx ; x points to temp6, d = val1*val3 + carry + (val1*val4 + val2*val3 + val4*val2/256)/256
  7981. rts ;
  7982.  
  7983.  
  7984.  
  7985. ;******************************************************************
  7986. ;
  7987. ;
  7988. ; fractional 32 bit by 16 bit division:
  7989. ; input: D:temp2:temp3 = 32 bit numerator;
  7990. ; temp4:temp5 = 16 bit denominator
  7991. ;
  7992. ; output: D = quotient
  7993. ; X = remainder
  7994. ;
  7995. ;******************************************************************
  7996. div3216 cmpd1 temp4 ;
  7997. bcs L1582 ; Branch if denominator > numerator (results<1)
  7998. ldd #$ffff ; denominator<=numerator, return .99998....
  7999. bra L1586 ;
  8000.  
  8001. L1582 ldy #$0010 ; Loop 16 times
  8002. ldx temp2
  8003. L1583 xgdx
  8004. asld
  8005. xgdx
  8006. rolb
  8007. rola
  8008. bcs L1584
  8009. cmpd1 temp4
  8010. bcs L1585
  8011. L1584 subd temp4
  8012. inx
  8013. L1585 decy
  8014. bne L1583
  8015. xgdx
  8016. L1586 rts
  8017.  
  8018.  
  8019.  
  8020. ;******************************************************************
  8021. ;
  8022. ;
  8023. ; 2D table lookup/interpolation
  8024. ;
  8025. ; Input:
  8026. ; temp6 = in1 (column index*16) (optionally in a if using 2Dlookup2)
  8027. ; temp7 = in2 (row index*16) (optionally in b if using 2Dlookup2)
  8028. ; hi(Y) = scale, number of elements per row in the table
  8029. ; lo(Y) = offset, added to value loaded from table before interpolation
  8030. ; X = 2Dtable address
  8031. ;
  8032. ; Output: A and B contain the same 2D interpolated value from the table
  8033. ;
  8034. ;
  8035. ;
  8036. ;******************************************************************
  8037. lookup2D ldd temp6 ; a=in1, b=in2
  8038. lookup2D2 sty temp3 ; temp3 = scale temp4=offset
  8039. staa temp5 ; temp5 = in1
  8040. clra ; a=0, b=in2
  8041. asld ;
  8042. asld ;
  8043. asld ;
  8044. asld ; d = in2*16
  8045. pshb ; st0 = LO(in2*16)
  8046. ldab temp3 ; b=scale
  8047. mul ; d = in2*16/256*scale
  8048. stx temp1 ; temp1:temp2 = 2Dtable
  8049. addd temp1 ; d = 2Dtable + in2*16/256*scale
  8050. std temp1 ; temp1:temp2 = 2Dtable + in2*16/256*scale
  8051. ldx temp1 ; X = 2Dtable + in2*16/256*scale, first row of interpolation
  8052. pshx ; st1:st2 = 2Dtable + in2*16/256*scale
  8053. ldab temp3 ; b=scale
  8054. abx ; X = 2Dtable + in2*16/256*scale + scale, go to next row of interpolation...
  8055. bsr L1589 ; Calculate interpolated value on second row, result in b
  8056. stab temp3 ; temp3 = interpolated value on second row
  8057. pulx ; x = 2Dtable + in2*16/256*scale (first row)
  8058. bsr L1589 ; Calculate interpolated value on first row, result in b
  8059. stab temp2 ; temp3 = interpolated value on first row
  8060. pula ; a = LO(in2*16)
  8061. ldx #temp2 ;
  8062. bra interpSpec ; Interpolate temp variables temp2 and temp3 using fractional part a=LO(in2*16) with no scaling on a and return to calling sub, result in b
  8063.  
  8064.  
  8065.  
  8066. ;******************************************************************
  8067. ;
  8068. ;
  8069. ; Used by lookup2D, interpolate on one row between two columns
  8070. ;
  8071. ;
  8072. ;******************************************************************
  8073. L1589 ldaa temp5 ; a = in1
  8074. clrb ; b=0
  8075. lsrd ;
  8076. lsrd ;
  8077. lsrd ;
  8078. lsrd ; d = in1*256/16 = in1*16
  8079. pshb ; st3 = LO(in1*16)
  8080. tab ; b=in1*16/256=in1/16
  8081. pula ; a=LO(in1*16)
  8082. abx ; X = startOfRow + in1/16
  8083. ldab $00,x ; b = val1, table lookup, get value in row
  8084. addb temp4 ; b = val1 + offset
  8085. stab temp1 ; temp1 = val1 + offset
  8086. ldab $01,x ; b = val2, next value in table on same row
  8087. addb temp4 ; b = val2 + offset
  8088. stab temp2 ; temp2 = val2 + offset
  8089. ldx #temp1 ; X points to temp variables
  8090. bra interpSpec ; Interpolate temp variables temp1 and temp2 using fractional part a=LO(in1*16) with no scaling on a and return to calling sub, result in b
  8091.  
  8092.  
  8093.  
  8094. ;******************************************************************
  8095. ;
  8096. ; Interpolate a table using ectCond, (ectCond is inv. proportional to temp)
  8097. ; This one compensate for non constant distance between the table points
  8098. ;
  8099. ; Used in the calculation of injPwStart
  8100. ; Input X = table
  8101. ;
  8102. ;
  8103. ;
  8104. ;******************************************************************
  8105. interpEct2 ldab ectCond ; b = ectCond (ect conditionned for table interp..)
  8106. cmpb #$c0 ; -7degC
  8107. bcs L1591 ; Branch if ectCond < $c0
  8108. clra ; a = 0, d = ectCond
  8109. asld ; d = 2*ectCond
  8110. subd #$00c0 ; d = 2*ectCond -$c0
  8111. jsr ovfCheck ;
  8112. L1591 tba ; a = 2*ectCond -$c0
  8113. bra interp32 ;
  8114.  
  8115.  
  8116.  
  8117. ;******************************************************************
  8118. ;
  8119. ;
  8120. ; Interpolate a table using ECT (ectCond is inv. proportional to temp)
  8121. ; Input X = table
  8122. ;
  8123. ;
  8124. ;******************************************************************
  8125. interpEct ldaa ectCond
  8126. bra interp32
  8127.  
  8128.  
  8129.  
  8130. ;******************************************************************
  8131. ;
  8132. ;
  8133. ;
  8134. ;
  8135. ;
  8136. ;******************************************************************
  8137. interp16rpm ldaa rpmIndex1
  8138. bra interp16b
  8139.  
  8140.  
  8141.  
  8142. ;******************************************************************
  8143. ;
  8144. ;
  8145. ; Interpolate table using Conditionned IAT (iatCond)
  8146. ;
  8147. ;
  8148. ;******************************************************************
  8149. iatCInterp ldaa iatCond
  8150. bra interp32
  8151.  
  8152.  
  8153.  
  8154. ;******************************************************************
  8155. ;
  8156. ;
  8157. ;
  8158. ;
  8159. ;
  8160. ;******************************************************************
  8161. interp16b clrb
  8162. bra interp16
  8163.  
  8164.  
  8165.  
  8166. ;******************************************************************
  8167. ;
  8168. ; Table lookup with fractional interpolation
  8169. ; input value = A
  8170. ; input table = X
  8171. ; output value in a and b is (interpolated table) = X(A/32)
  8172. ;
  8173. ;
  8174. ;******************************************************************
  8175. interp32 clrb ;
  8176. lsrd ;
  8177. interp16 lsrd ;
  8178. lsrd ;
  8179. lsrd ;
  8180. lsrd ; A=A/32, B = 5 LSB of A in upper part
  8181. interp1 pshb ; \
  8182. tab ; >transfer A to B, only 3 bits left
  8183. pula ; / At this point, B=integer part of input/32, A=fractional part
  8184. abx ; ADD B TO X: B = 0 to 7 (3 bits) (integer table lookup), X=V(0)
  8185.  
  8186. interpSpec tsta ;
  8187. beq L1601 ; Branch if a = 0 (no fractional interp)
  8188. ldab $01,x ; B=V(1)
  8189. subb $00,x ; B=V(1)-V(0)
  8190. bcc L1600 ; banch if V(1)-V(0) > 0
  8191. inx ; negative->X=V(1)
  8192. negb ;
  8193. nega ;
  8194. L1600 mul ; D=A*B (
  8195. aslb ; shift left B (... carry)
  8196. L1601 adca $00,x ; add to a with carry
  8197. tab
  8198. rts
  8199.  
  8200.  
  8201.  
  8202. ;******************************************************************
  8203. ;
  8204. ; Interpolate table an multiply result by [temp6:temp7]
  8205. ;
  8206. ;
  8207. ;
  8208. ;******************************************************************
  8209. interp32mul bsr interp32
  8210. ; continued below...
  8211.  
  8212.  
  8213.  
  8214. ;******************************************************************
  8215. ;
  8216. ; Multiply 8 bit by 16 bits, final result is scaled by 128,
  8217. ; rounded and checked for overflow
  8218. ;
  8219. ; input:
  8220. ; b: 8 bit value
  8221. ; [temp6:temp7]: 16 bit value
  8222. ;
  8223. ; output:
  8224. ; D and [temp6:temp7]: rounded 2*b*[temp6:temp7]/256 = b*[temp6:temp7]/128
  8225. ;
  8226. ;
  8227. ;******************************************************************
  8228. mul816_128 jsr mul816 ; D = b*[temp6:temp7]/256
  8229. asl temp3 ; Get lowest bit from fractional part
  8230. rolb ; Shift it in
  8231. rola ; shift it in, D = 2*b*[temp6:temp7]/256
  8232. bcs L1604 ; Branch if overflow
  8233. asl temp3 ; No overflow, Get second lowest bit from fractional part
  8234. adcb #$00 ; Add it to result (round-up result)
  8235. adca #$00 ; propagate
  8236. bcc L1605 ; Branch if no overflow
  8237. L1604 ldaa #$ff ; Overflow, return max value
  8238. L1605 std temp6 ; Store result
  8239. rts ;
  8240.  
  8241.  
  8242.  
  8243. ;******************************************************************
  8244. ;
  8245. ; Input:
  8246. ; D = 16 bit value1 (should be only 11 or 12 bits???)
  8247. ; temp6:temp7 = 16 bit value2
  8248. ;
  8249. ; Output:
  8250. ; D and [temp6:temp7] = rounded value1*value2/128 (a=$ff in case of overflow)
  8251. ; Optionally, divided by and additional 4 if mul1616_512 is used or 16 if mul1616_2K is used
  8252. ;
  8253. ;******************************************************************
  8254. mul1616_128 asld ;
  8255. asld ;
  8256. mul1616_512 asld ;
  8257. asld ; D = value1 * 16
  8258. mul1616_2K jsr mul1616 ; D = (value1 * 16 * value2)/65536 (upper 16 bits of mul1616)
  8259. ldx temp2 ; X = lower 16 bits of (value1 * 16 * value2)
  8260. xgdx ; D = lower 16 bits of (value1 * 16 * value2), X = (value1 * 16 * value2)/65536
  8261. ldab #$03 ; a = 3rd part of mul1616, b=3=number of loop to execute
  8262.  
  8263. ;--------------------------------------------
  8264. ; Loop 3 times to divide 24 bit result by 8
  8265. ;--------------------------------------------
  8266. L1609 xgdx ; Xhi = 3rd part of mul1616, Xlo = 3, D = (value1 * 16 * value2)/65536
  8267. lsrd ; D = (value1 * 16 * value2)/65536/2^n, carry = lower bit
  8268. xgdx ; X = (value1 * 16 * value2)/65536/2^n, a = 3rd part of mul1616/2^(n-1), b=3 on first loop
  8269. rora ; shift in carry, a = (3rd part of mul1616)/2^n, b=3 on first loop
  8270. decb ; b=b-1
  8271. bne L1609 ; loop
  8272.  
  8273. ;------------------------------------------------------------
  8274. ; Round-up and check for overflow in A
  8275. ; At this point, 24 bit result should have been
  8276. ; shifted down to fit only in 16 bits, upper 8 bit
  8277. ; should be 0, if not, b will be loaded with $ff in scale1m
  8278. ;------------------------------------------------------------
  8279. adca #$00 ; Round-up result 3rd part of result
  8280. psha ; st0 = lower 8 bit of result
  8281. xgdx ; D = (value1 * 16 * value2)/65536/8
  8282. bsr scale1m ; Propagate carry from 3rd part of result to D and check for overflow
  8283.  
  8284. ;-----------------------------------------------------------------------
  8285. ; At this point, we assume upper 8 bits=0 and only keep lower 16 bits
  8286. ;-----------------------------------------------------------------------
  8287. tba ; a = 2nd part of 24 bit result
  8288. pulb ; b = lower 8 bit of 24 bit result, in short, D=(value1 * 16 * value2)/256/8 = value1*value2/128
  8289. std temp6 ; temp6:temp7 = value1*value2/128
  8290. rts ;
  8291.  
  8292.  
  8293.  
  8294. ;******************************************************************
  8295. ;
  8296. ; Scaling function with rounding
  8297. ; Divide d by 128 or 64, etc.
  8298. ;
  8299. ;
  8300. ;
  8301. ;******************************************************************
  8302. scale128 lsrd ;
  8303. scale64 lsrd ;
  8304. lsrd ;
  8305. scale16 lsrd ;
  8306. scale8 lsrd ;
  8307. lsrd ;
  8308. lsrd ;
  8309. adcb #$00 ; Round-up by adding carry bit
  8310. adca #$00 ; propagate addition
  8311. rts ;
  8312.  
  8313.  
  8314.  
  8315. ;******************************************************************
  8316. ;
  8317. ; Input:
  8318. ; d = v1:v0
  8319. ;
  8320. ; Output:
  8321. ; a = v1 rounded up depending on v0, up to max of $ff
  8322. ;
  8323. ;
  8324. ;******************************************************************
  8325. round256 aslb ; Shift high bit of b in carry
  8326. adca #$00 ; roundup a
  8327. bcc L1615 ; branch if no overflow
  8328. deca ; Overflow, use max of $ff
  8329. L1615 rts ;
  8330.  
  8331.  
  8332.  
  8333. ;******************************************************************
  8334. ;
  8335. ;
  8336. ; Divide D by 128 or 64 or 32... in order for the value to fit
  8337. ; only in b (with a=0). If it does not fit (a<>0), b is loaded with
  8338. ; max value of $ff
  8339. ;
  8340. ;
  8341. ;
  8342. ;******************************************************************
  8343. scale128m lsrd
  8344. scale64m lsrd
  8345. lsrd
  8346. scale16m lsrd
  8347. scale8m lsrd
  8348. scale4m lsrd
  8349. scale2m lsrd ; D = D/128, carry contains last bit shifted out
  8350. scale1m adcb #$00 ; Add last bit shifted out (round-up number)
  8351. adca #$00 ; propagate addition of last bit
  8352. ovfCheck tsta
  8353. beq L1624 ; branch if A=0
  8354. ldab #$ff ; A is not 0, set fractional part to FF (the only one returned...)
  8355. L1624 rts
  8356.  
  8357.  
  8358.  
  8359. ;******************************************************************
  8360. ;
  8361. ;
  8362. ; Apply an offset and clip input, the result is usually used to
  8363. ; interpolate a table with a value of known range...
  8364. ;
  8365. ; in Xhi = max value
  8366. ; in Xlo = offset
  8367. ; in b = input value
  8368. ;
  8369. ; out b = max(min(b,Xhi)-Xlo,0)
  8370. ;
  8371. ;
  8372. ;******************************************************************
  8373. clipOffset stx temp1
  8374. cmpb temp1
  8375. bcs L1626
  8376. ldab temp1
  8377. L1626 subb temp2
  8378. bcc L1627
  8379. clrb
  8380. L1627 rts
  8381.  
  8382.  
  8383.  
  8384. ;******************************************************************
  8385. ;
  8386. ; This function is used to apply a piecewise linear transformation
  8387. ; to the input value, see L2052, L2053 and L2054. This can be seen
  8388. ; as calculating the index into a table where the spacing between
  8389. ; table entries is not constant (first three entries spaced by x,
  8390. ; next three spaced by 2x, etc...)
  8391. ;
  8392. ; Input:
  8393. ; a = val
  8394. ; b = 0 (ignored or cleared)
  8395. ; y = points to table with first two values being a max
  8396. ; and offset and the rest being triplets (addVal, nshift, compVal)
  8397. ; where compVal is monotonicaly increasing...
  8398. ;
  8399. ; Output:
  8400. ; a = (min(val,max) - offset + addVal)/2^(nshift-1)
  8401. ;
  8402. ; where max and offset are the first two values of the table
  8403. ; and where addVal and nShift are taken from the table according to val-offset
  8404. ;
  8405. ;******************************************************************
  8406. ;------------------------------------------------------
  8407. ; Apply max value of table, a = min(val,table[0])
  8408. ;------------------------------------------------------
  8409. pwiseLin cmpa $00,y ; Operation does ++y...I assume
  8410. bcs L1629 ; Branch if a < table[0]
  8411. decy ; --y, go back to 0
  8412. ldaa $00,y ; a=table[y++]
  8413. clrb ; b=0
  8414.  
  8415. ;----------------------------------------------------------
  8416. ; Subtract offset of table, a = val-table[1] = val-offset
  8417. ;----------------------------------------------------------
  8418. L1629 suba $00,y ; a = val-table[y++] = val-offset
  8419. bcc L1630 ; Branch if result positive
  8420. clra ; Result negative, d=0
  8421. clrb ; d=0
  8422.  
  8423. ;----------------------------------------------
  8424. ; Loop until we find compVal > a
  8425. ;----------------------------------------------
  8426. L1630 ldx $00,y ; x = table[y]:table[y+1]; y = y + 2
  8427. cmpa $00,y ; Operation does ++y...I assume
  8428. bcc L1630 ; Loop if a >= table[y]
  8429.  
  8430. ;--------------------------------------------------------------
  8431. ; Store the corresponding addVal and nshift in temp1 and temp2
  8432. ;--------------------------------------------------------------
  8433. stx temp1 ; temp1 = addVal;, temp2 = nshift
  8434.  
  8435. ;--------------------------------------------
  8436. ; Add addVal to a, a = val - offset + addVal
  8437. ;--------------------------------------------
  8438. adda temp1 ; a = a-offset+addVal
  8439.  
  8440. ;----------------------------------------------------------------
  8441. ; Apply nshift to a, a = (val - offset + addVal)/2^(nshift-1)
  8442. ;----------------------------------------------------------------
  8443. L1631 dec temp2 ; temp2=table[y+1]-1
  8444. beq L1632 ; Btanch if table[y+1]-1 =0
  8445. lsrd ; d = d/2
  8446. bra L1631 ; Loop
  8447. L1632 rts ;
  8448.  
  8449.  
  8450.  
  8451. ;******************************************************************
  8452. ;
  8453. ; Input:
  8454. ; y: point to piecewise linear transformation table
  8455. ;
  8456. ; Apply piecewise linear transformation to rpm4 and scale
  8457. ;
  8458. ;
  8459. ;******************************************************************
  8460. rpmPwise ldd rpm4 ;
  8461. asld ;
  8462. asld ; d = rpm4*4
  8463. bsr pwiseLin ;
  8464. bra scale16m ;
  8465.  
  8466.  
  8467.  
  8468. ;******************************************************************
  8469. ;
  8470. ;
  8471. ; Decrement value at X by 1 (min of 0) if Tclocks.0 (40Hz signal) is set
  8472. ;
  8473. ;
  8474. ;******************************************************************
  8475. decX40Hz ldab #$01
  8476. brclr Tclocks, #$01, L1637
  8477.  
  8478.  
  8479.  
  8480. ;******************************************************************
  8481. ;
  8482. ;
  8483. ; Decrement all values in a given table by 1, stop at min value 0
  8484. ; X points to table, b is number of elements
  8485. ;
  8486. ;
  8487. ;******************************************************************
  8488. decTable brclr $0000,x,#$ff,L1636
  8489. dec $0000,x
  8490. L1636 inx
  8491. decb
  8492. bne decTable
  8493. L1637 rts
  8494.  
  8495.  
  8496.  
  8497. ;******************************************************************
  8498. ;
  8499. ; Lookup the given table using config2
  8500. ; lowest 2 bits as index to 16 bits values
  8501. ;
  8502. ; Input:
  8503. ; x: points to table
  8504. ;
  8505. ; Output:
  8506. ;
  8507. ; x(16 bits): table(2*(config2 & $03))
  8508. ;
  8509. ;******************************************************************
  8510. cfgLookup16 ldab config2 ; b = config2
  8511. andb #$03 ;
  8512. aslb ; b = 2*(config2 & $03)
  8513. abx ; x points to table(2*(config2 & $03))
  8514. ldx $00,x ; x = table(2*(config2 & $03))
  8515. rts ;
  8516.  
  8517.  
  8518.  
  8519. ;******************************************************************
  8520. ;
  8521. ; Read value from IO port (ADC) using a and b (d) as the values
  8522. ; to write to adc_ctl and adc_data respectively. a should be set
  8523. ; to the port number (0 to 7 as indicated below) with the start bit
  8524. ; set to 1 ($08)
  8525. ;
  8526. ; Port $00: ECT (engine coolant temp)
  8527. ; Port $01: IAT
  8528. ; Port $02: BARO
  8529. ; Port $03: O2
  8530. ; Port $04: EGRT
  8531. ; Port $05: BATT
  8532. ; Port $06: KNOCK count
  8533. ; Port $07: TPS
  8534. ;
  8535. ;
  8536. ;******************************************************************
  8537. readAdc1 sei ; set interrupt mask
  8538. readAdc2 std adc_ctl ; set port #
  8539. brn L1641 ; time delay
  8540. L1641 div airCntDef ; time delay
  8541. mul ; time delay
  8542. mul ; time delay
  8543. mul ; time delay
  8544. mul ; time delay
  8545. mul ; time delay
  8546. mul ; time delay
  8547. mul ; time delay
  8548. ldd adc_ctl ; get port value
  8549. rts
  8550.  
  8551.  
  8552.  
  8553. ;******************************************************************
  8554. ;
  8555. ;
  8556. ; input: rpm in b, maxRpm in a
  8557. ; (note that airVol is also used instead of rpm in
  8558. ; calling this function)
  8559. ;
  8560. ; output: b = min(max(rpm-$10,0), maxRpm) $10->500rpm
  8561. ;
  8562. ;******************************************************************
  8563. rpmRange subb #$10 ;
  8564. bcc abmin ; Branch if result positive
  8565. clrb ; Use min of 0
  8566.  
  8567. ;--------------------------------------------
  8568. ; Code below also called as a function
  8569. ; abmin -> b = minimum of a and b (can also
  8570. ; be seen as applying a max...)
  8571. ;--------------------------------------------
  8572. abmin cba ; compare (A-B)
  8573. bcc L1644 ; Branch if b<=maxRpm
  8574. tab ; b = maxRpm
  8575. L1644 rts ; return
  8576.  
  8577.  
  8578.  
  8579. ;******************************************************************
  8580. ;
  8581. ; Input:
  8582. ; None (a=airVol if called from L1647)
  8583. ;
  8584. ; Output: in b (same result in a):
  8585. ;
  8586. ; airVol[T]B < 60: airVol[T]B - $20
  8587. ; airVol[T]B >= 60: 0.668 * airVol[T]B
  8588. ;
  8589. ; The maximum of airVolTB or airVolB is used, see comments below
  8590. ;
  8591. ;----------------------------------------
  8592. ;
  8593. ; the ecu makes a temp value for the table lookup..
  8594. ; so if 0xe3 <= 96, value = 0xe3 - 32,
  8595. ; if 0xe3 > 96, value = 0xe3 * 2/3
  8596. ;
  8597. ;******************************************************************
  8598. getLoadForMaps
  8599. #ifdef extLoadRange
  8600. #ifdef extLoadRange2
  8601. ldaa L0054 ; a = airVol16/4
  8602. ldab baroFact ; b = baroFact
  8603. mul ; d = airVol16/4 * baroFact
  8604. jsr scale128m ; b = airVol16/4 * baroFact/128
  8605. ldaa iatCompFact ; a = iatCompFact
  8606. bpl test123 ; Branch if iatCompFact < $80
  8607. mul ; d = airVol16/4 * baroFact/128 * iatCompFact
  8608. jsr scale128m ; b = airVol16/4 * baroFact/128 * iatCompFact/128
  8609. test123 tba ; a = airVol16/4 * baroFact/128 [* iatCompFact/128]
  8610. #else
  8611. ldaa L0054 ; a = airVol16/4
  8612. bra L1647
  8613. nop
  8614. nop
  8615. nop
  8616. nop
  8617. nop
  8618. nop
  8619. nop
  8620. #endif
  8621. #else
  8622. ;-----------------------------------------------------
  8623. ; Decide on which value to use, airVolTB or airVolB
  8624. ;
  8625. ; Done this way, it correspond to taking the
  8626. ; highest of the two since:
  8627. ;
  8628. ; airVolTB = airVolB * iatCompFact/128
  8629. ;
  8630. ; if iatCompFact<128 -> max is airVolB
  8631. ; if iatCompFact>=128 -> max is airVolTB
  8632. ;
  8633. ; Since the load is used to interpolate the fuel map,
  8634. ; it is probably safer to use the maximum of the two
  8635. ; value, i.e. assume there is more air than less and
  8636. ; therefore risk of running too rich. The risk of using
  8637. ; only airVolTB is that if the temperature sensor is
  8638. ; heat soaked then it will report a higher temperature
  8639. ; than the actual temperature of the air getting in.
  8640. ; This means that airVolTB will report less air than
  8641. ; is actually getting in and there won't be enough
  8642. ; fuel injected -> we will run too lean...
  8643. ;-----------------------------------------------------
  8644. ldaa iatCompFact ; a=iatCompFact ($80=100%)
  8645. bpl L1646 ; Branch if iatCompFact<100%
  8646. ldaa airVolTB ; iatCompFact>=100%, a = airVolTB
  8647. bra L1647 ;
  8648. L1646 ldaa airVolB ; iatCompFact<100%, a = airVolB
  8649. #endif
  8650.  
  8651. ;-------------------------------------
  8652. ; Compute load index from air volume
  8653. ;-------------------------------------
  8654. L1647 tab ; a = b = airVol[T]B
  8655. subb #$60 ; b = airVol[T]B - $60
  8656. bcs L1648 ; branch if underflow (airVol[T]B <$60)
  8657. ldaa #$ab ; a = $ab
  8658. mul ; d = $ab * (airVol[T]B - $60)
  8659. aslb ; Shift higher bit of b in carry
  8660. adca #$00 ; Round-up a
  8661. adda #$60 ; a = rounded $ab * (airVol[T]B - $60)/256 + $60
  8662. L1648 suba #$20 ; a = a-20
  8663. bcc L1649 ; branch if no overflow
  8664. clra ; use min value
  8665. L1649 tab ; a = b = result
  8666. rts ;
  8667.  
  8668.  
  8669.  
  8670. ;******************************************************************
  8671. ;
  8672. ; Input: temp22:temp23 = v1:v0 (set to Tcas)
  8673. ;
  8674. ; Output
  8675. ; d = abs([temp22:temp23] - TcasLast0) - [temp22:temp23] * $30//256
  8676. ;
  8677. ;******************************************************************
  8678. L1650 ldaa temp22 ; a = v1
  8679. ldab #$30 ; b = $30
  8680. mul ; d = $30 * v1
  8681. xgdx ; x = $30 * v1
  8682. ldaa temp23 ; a = v0
  8683. ldab #$30 ; b = $30
  8684. mul ; d = $30 * v0
  8685. tab ; b = $30 * v0 /256
  8686. abx ; x = $30 * v1 + $30 * v0 /256 = $30*(v1+v0/256) = [temp22:temp23] * $30//256
  8687. stx temp20 ; temp20 = [temp22:temp23] * $30//256
  8688.  
  8689. ;---------------------------------------
  8690. ; Compute abs(temp22:temp23 - TcasLast0)
  8691. ;---------------------------------------
  8692. ldd temp22 ; d = temp22:temp23
  8693. subd TcasLast0 ; d = temp22:temp23 - TcasLast0
  8694. bcc L1651 ; Branch if result positive
  8695. coma ; Result negative, make it positive
  8696. comb ;
  8697. addd #$0001 ; two's complement...
  8698.  
  8699. ;----------------
  8700. ; Compute result
  8701. ;----------------
  8702. L1651 subd temp20 ; d = abs(temp22:temp23 - TcasLast0) - [temp22:temp23] * $30//256
  8703. rts ;
  8704.  
  8705.  
  8706.  
  8707. ;******************************************************************
  8708. ;
  8709. ;
  8710. ; Input capture interrupt 1
  8711. ;
  8712. ; Main CAS interrupt routine, triggered on both the rising
  8713. ; and falling edge of the CAS signal (edge detection polarity
  8714. ; is toggled in the code upon each interrupt)
  8715. ;
  8716. ;
  8717. ;******************************************************************
  8718. ;------------------------------------------------
  8719. ; Read t3_clock1 for later use and then
  8720. ; read t1_csr (not used so I assume it ackowledges
  8721. ; the interrupt ?)
  8722. ;------------------------------------------------
  8723. inCaptInt1 ldx t3_clock1 ; Get current coil clock value when the cas edge changed?
  8724. ldab t1_csr ; Acknowledge cas input capture interrupt?
  8725.  
  8726. ;--------------------------------------------------------
  8727. ; Update t1_lastCas for eventual aiflow calculation
  8728. ;--------------------------------------------------------
  8729. ldd t1_inCapt ; Get cas input timer capture high/low
  8730. std t1_lastCas ; store it here
  8731.  
  8732. ;------------------------------------------------
  8733. ; Update temp20 assuming interrupt is from t2
  8734. ; will be changed later if assumption was wrong
  8735. ;------------------------------------------------
  8736. ldd t3_clock2 ; Get current counter value
  8737. std temp20 ; temp20 = t3_clock2
  8738.  
  8739. ;-------------------------------------------
  8740. ; Branch to rising or falling edge section
  8741. ; depending on the interrupt source
  8742. ;-------------------------------------------
  8743. brclr t1_csr, #$02, casRiseProc ; Branch if this is the cas rising edge
  8744. jmp casFallProc ; Branch to cas failling edge section
  8745.  
  8746.  
  8747.  
  8748. ;******************************************************************
  8749. ;
  8750. ;
  8751. ; Section processing the CAS interrupt on the rising edge
  8752. ;
  8753. ;;
  8754. ;
  8755. ;******************************************************************
  8756. ;-------------------------------------------------------------------------
  8757. ; Check which of t3_clock1 or t3_clock2 should be used?
  8758. ; Not sure what that bit means???????????
  8759. ;-------------------------------------------------------------------------
  8760. casRiseProc brclr t3_csr0, #$10, L1654 ; Branch if we should use t3_clock2, nothing to do, that's what we assumed above
  8761.  
  8762. ;-------------------------------------------------------------------------
  8763. ; t3_clock1 should be used, our assumption that it was
  8764. ; t3_clock2 was wrong, update d and temp20 with the correct values
  8765. ;-------------------------------------------------------------------------
  8766. xgdx ; d = t3_clock1
  8767. std temp20 ; temp20 = t3_clock1
  8768.  
  8769. ;-------------------------------------------------------------
  8770. ; Branch to rest of code if the time between CAS
  8771. ; interrupts makes sense (rpm is not too high...)
  8772. ;
  8773. ; The time measured here is the time in-between cas
  8774. ; pulses since it is measured from the falling edge to the
  8775. ; rising edge. Since this time correspond to 110deg
  8776. ; then the 1ms below correspond to 360/110*1ms = 3.27ms
  8777. ; per rotation which correspond to 18333rpm. The threshold of
  8778. ; 1ms or 0.5 ms (18333 or 23333rpm) on the cas rising and falling
  8779. ; edge section is not the same and it might be due to the fact
  8780. ; that on the cas falling edge, we measure a smaller interval
  8781. ; (70deg instead of 110deg) and therefore the uncertainty is
  8782. ; higher???
  8783. ;-------------------------------------------------------------
  8784. L1654 subd casFallTime0 ; d = t3_clock1 - casFallTime0
  8785. cmpd #$00fa ; 1ms at 250KHz
  8786. bcc L1655 ; Branch if (t3_clock1 or t3_clock2 - casFallTime0) >= $00fa
  8787.  
  8788. ;------------------------------------------------
  8789. ; RPM seems too high to make sense, check if it is
  8790. ; not instead because RPM is so low that the 16 bit
  8791. ; counter subtraction above rolled-over.
  8792. ;
  8793. ; Branch to rest of code if the T200_casFall timer shows
  8794. ; that rpm is very low...
  8795. ;------------------------------------------------
  8796. ldaa T200_casFall ;
  8797. cmpa #$0e ; 70ms at 200Hz
  8798. bcs L1655 ; branch if T200_casFall<70ms, T200_casFall is init with 265ms, the time between interrupt is very high
  8799.  
  8800. ;-------------------------------------------------------------
  8801. ; Time between interrupts doesn't make sense, just ignore it
  8802. ; Return from interrupt
  8803. ;-------------------------------------------------------------
  8804. rti ;
  8805.  
  8806. ;---------------------------------------------------------------
  8807. ; Update temp22:temp23 = Tcas measured on the cas rising edge
  8808. ;---------------------------------------------------------------
  8809. L1655 ldd temp20 ; D = temp20
  8810. subd casRiseTime0 ; D = temp20-casRiseTime0(old counter) = Tcas = 250000/2/(rpm/60)
  8811. std temp22 ; temp22:temp23 = Tcas (temp22 is not dedicated for that purpose...)
  8812.  
  8813. ;--------------------------------
  8814. ; Validate temp22:temp23 = Tcas
  8815. ;--------------------------------
  8816. ldab T200_casRise ;
  8817. beq L1656 ; Branch if timer expired (very long Tcas...)
  8818. tsta ;
  8819. bmi L1657 ; Branch if Tcas/256 >= 128 (rpm<229)
  8820. cmpb #$0e ;
  8821. bhi L1657 ; Branch if T200_casRise > $0e (70ms)
  8822. L1656 ldd #$ffff ; Use max Tcas
  8823. std temp22 ; store Tcas
  8824.  
  8825. ;----------------------------------------------
  8826. ; Section to calculate new casFlags0 value but do
  8827. ; not store it now. It will be stored once the
  8828. ; interrupt is validated
  8829. ;----------------------------------------------
  8830. ;----------------------------------------------
  8831. ; First transfer some bits to "old" positions
  8832. ; and assume some bits will be set...
  8833. ;----------------------------------------------
  8834. L1657 ldaa casFlags0 ; a = casFlags0
  8835. anda #$05 ; a = casFlags0 & 00000101 (keep some old bits)
  8836. asla ; Move the old bits to the "old" positions
  8837. oraa #$35 ; preload some bits (00110101)
  8838.  
  8839. ;--------------------------------------------------
  8840. ; Set flag if timing adjustement mode is active
  8841. ;--------------------------------------------------
  8842. brclr timAdjFlags, #$80, L1658 ; Branch if we are not in timing adjustement mode
  8843. oraa #$40 ; a = (casFlags0 & 00001001)*2 | 01110101
  8844.  
  8845. ;-------------------------------------------------
  8846. ; Reset some new casFlags0 bits depending on rpm
  8847. ;-------------------------------------------------
  8848. L1658 ldx temp22 ; x = temp22:temp23 = Tcas
  8849. cpx #$061a ; 4801rpm
  8850. bcs L1662 ; Branch if rpm(Tcas) > 4801rpm
  8851. anda #$df ; Reset 00100000
  8852. cpx #$1081 ; 1775rpm
  8853. bcs L1659 ; Branch if rpm(Tcas) > 1775rpm
  8854. anda #$fb ; Reset 00000100
  8855. L1659 cpx #$1306 ; 1540rpm
  8856. bcs L1660 ; Branch if rpm(Tcas) > 1540rpm
  8857. anda #$ef ; reset 00010000
  8858.  
  8859. ;---------------------------------------
  8860. ; At this point rpm(Tcas) <= 4801rpm
  8861. ; Choose rpm threshold with hysteresis
  8862. ;---------------------------------------
  8863. L1660 ldab #$49 ; b = $49 (401rpm)
  8864. bita #$02 ;
  8865. bne L1661 ; Branch if bit was already set
  8866. ldab #$3a ; use lower threshold if bit alread set b = $3a (505rpm)
  8867.  
  8868. ;---------------------------------------------------------------------
  8869. ; Reset flag bit if we are above/below threshold, with hysteresis
  8870. ;---------------------------------------------------------------------
  8871. L1661 cmpb temp22 ;
  8872. bhi L1662 ; Branch if rpm(Tcas) < b (401rpm or 505rpm)
  8873. anda #$fe ; Reset 00000001
  8874.  
  8875. ;-----------------------------------------------------------
  8876. ; Store new value of casFlags0 in temp location for now
  8877. ;-----------------------------------------------------------
  8878. L1662 staa temp24 ; Store new casFlags0 in temp memory for now
  8879.  
  8880. ;--------------------------------------------------------------------
  8881. ; At this point, we will check the CAS signal to make sure it stays
  8882. ; set until 56us after the start of the interrupt. I guess this might
  8883. ; be to filter eventual glitches in the CAS signal
  8884. ;--------------------------------------------------------------------
  8885. ldd temp20 ;
  8886. addd #$000e ; d = StartInterruptTime + $0e (56us)
  8887. L1663 brclr port5, #$01, L1664 ; Branch as long as CAS bit is clear (CAS signal is set)
  8888. rti ; CAS bit was set, Bail of interrupt
  8889. L1664 cmpd1 t3_clock1 ; Compare current time to time stored when we started the interrupt processing
  8890. bpl L1663 ; Loop if t3_clock1 < (temp20 + $0e (56us)), i.e. if its been less than 56us since interrupt was called
  8891.  
  8892.  
  8893.  
  8894. ;************