PageRenderTime 43ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/Arduino/libraries/TVout/video_gen.cpp

https://bitbucket.org/DeepHorizons/arduino-hid
C++ | 470 lines | 394 code | 39 blank | 37 comment | 19 complexity | c203b356714cb4732c68510c11ea4e03 MD5 | raw file
  1. /*
  2. Copyright (c) 2010 Myles Metzer
  3. Permission is hereby granted, free of charge, to any person
  4. obtaining a copy of this software and associated documentation
  5. files (the "Software"), to deal in the Software without
  6. restriction, including without limitation the rights to use,
  7. copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the
  9. Software is furnished to do so, subject to the following
  10. conditions:
  11. The above copyright notice and this permission notice shall be
  12. included in all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  15. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  17. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  18. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. OTHER DEALINGS IN THE SOFTWARE.
  21. */
  22. #include <avr/interrupt.h>
  23. #include <avr/io.h>
  24. #include "video_gen.h"
  25. #include "spec/video_properties.h"
  26. #include "spec/asm_macros.h"
  27. #include "spec/hardware_setup.h"
  28. //#define REMOVE6C
  29. //#define REMOVE5C
  30. //#define REMOVE4C
  31. //#define REMOVE3C
  32. int renderLine;
  33. TVout_vid display;
  34. void (*render_line)(); //remove me
  35. void (*line_handler)(); //remove me
  36. void (*hbi_hook)() = &empty;
  37. void (*vbi_hook)() = &empty;
  38. // sound properties
  39. volatile long remainingToneVsyncs;
  40. void empty() {}
  41. void render_setup(uint8_t mode, uint8_t x, uint8_t y, uint8_t *scrnptr) {
  42. display.screen = scrnptr;
  43. display.hres = x;
  44. display.vres = y;
  45. display.frames = 0;
  46. if (mode)
  47. display.vscale_const = _PAL_LINE_DISPLAY/display.vres - 1;
  48. else
  49. display.vscale_const = _NTSC_LINE_DISPLAY/display.vres - 1;
  50. display.vscale = display.vscale_const;
  51. //selects the widest render method that fits in 46us
  52. //as of 9/16/10 rendermode 3 will not work for resolutions lower than
  53. //192(display.hres lower than 24)
  54. unsigned char rmethod = (_TIME_ACTIVE*_CYCLES_PER_US)/(display.hres*8);
  55. switch(rmethod) {
  56. case 6:
  57. render_line = &render_line6c;
  58. break;
  59. case 5:
  60. render_line = &render_line5c;
  61. break;
  62. case 4:
  63. render_line = &render_line4c;
  64. break;
  65. case 3:
  66. render_line = &render_line3c;
  67. break;
  68. default:
  69. if (rmethod > 6)
  70. render_line = &render_line6c;
  71. else
  72. render_line = &render_line3c;
  73. }
  74. DDR_VID |= _BV(VID_PIN);
  75. DDR_SYNC |= _BV(SYNC_PIN);
  76. PORT_VID &= ~_BV(VID_PIN);
  77. PORT_SYNC |= _BV(SYNC_PIN);
  78. DDR_SND |= _BV(SND_PIN); // for tone generation.
  79. // inverted fast pwm mode on timer 1
  80. TCCR1A = _BV(COM1A1) | _BV(COM1A0) | _BV(WGM11);
  81. TCCR1B = _BV(WGM13) | _BV(WGM12) | _BV(CS10);
  82. if (mode) {
  83. display.start_render = _PAL_LINE_MID - ((display.vres * (display.vscale_const+1))/2);
  84. display.output_delay = _PAL_CYCLES_OUTPUT_START;
  85. display.vsync_end = _PAL_LINE_STOP_VSYNC;
  86. display.lines_frame = _PAL_LINE_FRAME;
  87. ICR1 = _PAL_CYCLES_SCANLINE;
  88. OCR1A = _CYCLES_HORZ_SYNC;
  89. }
  90. else {
  91. display.start_render = _NTSC_LINE_MID - ((display.vres * (display.vscale_const+1))/2) + 8;
  92. display.output_delay = _NTSC_CYCLES_OUTPUT_START;
  93. display.vsync_end = _NTSC_LINE_STOP_VSYNC;
  94. display.lines_frame = _NTSC_LINE_FRAME;
  95. ICR1 = _NTSC_CYCLES_SCANLINE;
  96. OCR1A = _CYCLES_HORZ_SYNC;
  97. }
  98. display.scanLine = display.lines_frame+1;
  99. line_handler = &vsync_line;
  100. TIMSK1 = _BV(TOIE1);
  101. sei();
  102. }
  103. // render a line
  104. ISR(TIMER1_OVF_vect) {
  105. hbi_hook();
  106. line_handler();
  107. }
  108. void blank_line() {
  109. if ( display.scanLine == display.start_render) {
  110. renderLine = 0;
  111. display.vscale = display.vscale_const;
  112. line_handler = &active_line;
  113. }
  114. else if (display.scanLine == display.lines_frame) {
  115. line_handler = &vsync_line;
  116. vbi_hook();
  117. }
  118. display.scanLine++;
  119. }
  120. void active_line() {
  121. wait_until(display.output_delay);
  122. render_line();
  123. if (!display.vscale) {
  124. display.vscale = display.vscale_const;
  125. renderLine += display.hres;
  126. }
  127. else
  128. display.vscale--;
  129. if ((display.scanLine + 1) == (int)(display.start_render + (display.vres*(display.vscale_const+1))))
  130. line_handler = &blank_line;
  131. display.scanLine++;
  132. }
  133. void vsync_line() {
  134. if (display.scanLine >= display.lines_frame) {
  135. OCR1A = _CYCLES_VIRT_SYNC;
  136. display.scanLine = 0;
  137. display.frames++;
  138. if (remainingToneVsyncs != 0)
  139. {
  140. if (remainingToneVsyncs > 0)
  141. {
  142. remainingToneVsyncs--;
  143. }
  144. } else
  145. {
  146. TCCR2B = 0; //stop the tone
  147. PORTB &= ~(_BV(SND_PIN));
  148. }
  149. }
  150. else if (display.scanLine == display.vsync_end) {
  151. OCR1A = _CYCLES_HORZ_SYNC;
  152. line_handler = &blank_line;
  153. }
  154. display.scanLine++;
  155. }
  156. static void inline wait_until(uint8_t time) {
  157. __asm__ __volatile__ (
  158. "subi %[time], 10\n"
  159. "sub %[time], %[tcnt1l]\n\t"
  160. "100:\n\t"
  161. "subi %[time], 3\n\t"
  162. "brcc 100b\n\t"
  163. "subi %[time], 0-3\n\t"
  164. "breq 101f\n\t"
  165. "dec %[time]\n\t"
  166. "breq 102f\n\t"
  167. "rjmp 102f\n"
  168. "101:\n\t"
  169. "nop\n"
  170. "102:\n"
  171. :
  172. : [time] "a" (time),
  173. [tcnt1l] "a" (TCNT1L)
  174. );
  175. }
  176. void render_line6c() {
  177. #ifndef REMOVE6C
  178. __asm__ __volatile__ (
  179. "ADD r26,r28\n\t"
  180. "ADC r27,r29\n\t"
  181. //save PORTB
  182. "svprt %[port]\n\t"
  183. "rjmp enter6\n"
  184. "loop6:\n\t"
  185. "bst __tmp_reg__,0\n\t" //8
  186. "o1bs %[port]\n"
  187. "enter6:\n\t"
  188. "LD __tmp_reg__,X+\n\t" //1
  189. "delay1\n\t"
  190. "bst __tmp_reg__,7\n\t"
  191. "o1bs %[port]\n\t"
  192. "delay3\n\t" //2
  193. "bst __tmp_reg__,6\n\t"
  194. "o1bs %[port]\n\t"
  195. "delay3\n\t" //3
  196. "bst __tmp_reg__,5\n\t"
  197. "o1bs %[port]\n\t"
  198. "delay3\n\t" //4
  199. "bst __tmp_reg__,4\n\t"
  200. "o1bs %[port]\n\t"
  201. "delay3\n\t" //5
  202. "bst __tmp_reg__,3\n\t"
  203. "o1bs %[port]\n\t"
  204. "delay3\n\t" //6
  205. "bst __tmp_reg__,2\n\t"
  206. "o1bs %[port]\n\t"
  207. "delay3\n\t" //7
  208. "bst __tmp_reg__,1\n\t"
  209. "o1bs %[port]\n\t"
  210. "dec %[hres]\n\t"
  211. "brne loop6\n\t" //go too loopsix
  212. "delay2\n\t"
  213. "bst __tmp_reg__,0\n\t" //8
  214. "o1bs %[port]\n"
  215. "svprt %[port]\n\t"
  216. BST_HWS
  217. "o1bs %[port]\n\t"
  218. :
  219. : [port] "i" (_SFR_IO_ADDR(PORT_VID)),
  220. "x" (display.screen),
  221. "y" (renderLine),
  222. [hres] "d" (display.hres)
  223. : "r16" // try to remove this clobber later...
  224. );
  225. #endif
  226. }
  227. void render_line5c() {
  228. #ifndef REMOVE5C
  229. __asm__ __volatile__ (
  230. "ADD r26,r28\n\t"
  231. "ADC r27,r29\n\t"
  232. //save PORTB
  233. "svprt %[port]\n\t"
  234. "rjmp enter5\n"
  235. "loop5:\n\t"
  236. "bst __tmp_reg__,0\n\t" //8
  237. "o1bs %[port]\n"
  238. "enter5:\n\t"
  239. "LD __tmp_reg__,X+\n\t" //1
  240. "bst __tmp_reg__,7\n\t"
  241. "o1bs %[port]\n\t"
  242. "delay2\n\t" //2
  243. "bst __tmp_reg__,6\n\t"
  244. "o1bs %[port]\n\t"
  245. "delay2\n\t" //3
  246. "bst __tmp_reg__,5\n\t"
  247. "o1bs %[port]\n\t"
  248. "delay2\n\t" //4
  249. "bst __tmp_reg__,4\n\t"
  250. "o1bs %[port]\n\t"
  251. "delay2\n\t" //5
  252. "bst __tmp_reg__,3\n\t"
  253. "o1bs %[port]\n\t"
  254. "delay2\n\t" //6
  255. "bst __tmp_reg__,2\n\t"
  256. "o1bs %[port]\n\t"
  257. "delay1\n\t" //7
  258. "dec %[hres]\n\t"
  259. "bst __tmp_reg__,1\n\t"
  260. "o1bs %[port]\n\t"
  261. "brne loop5\n\t" //go too loop5
  262. "delay1\n\t"
  263. "bst __tmp_reg__,0\n\t" //8
  264. "o1bs %[port]\n"
  265. "svprt %[port]\n\t"
  266. BST_HWS
  267. "o1bs %[port]\n\t"
  268. :
  269. : [port] "i" (_SFR_IO_ADDR(PORT_VID)),
  270. "x" (display.screen),
  271. "y" (renderLine),
  272. [hres] "d" (display.hres)
  273. : "r16" // try to remove this clobber later...
  274. );
  275. #endif
  276. }
  277. void render_line4c() {
  278. #ifndef REMOVE4C
  279. __asm__ __volatile__ (
  280. "ADD r26,r28\n\t"
  281. "ADC r27,r29\n\t"
  282. "rjmp enter4\n"
  283. "loop4:\n\t"
  284. "lsl __tmp_reg__\n\t" //8
  285. "out %[port],__tmp_reg__\n\t"
  286. "enter4:\n\t"
  287. "LD __tmp_reg__,X+\n\t" //1
  288. "delay1\n\t"
  289. "out %[port],__tmp_reg__\n\t"
  290. "delay2\n\t" //2
  291. "lsl __tmp_reg__\n\t"
  292. "out %[port],__tmp_reg__\n\t"
  293. "delay2\n\t" //3
  294. "lsl __tmp_reg__\n\t"
  295. "out %[port],__tmp_reg__\n\t"
  296. "delay2\n\t" //4
  297. "lsl __tmp_reg__\n\t"
  298. "out %[port],__tmp_reg__\n\t"
  299. "delay2\n\t" //5
  300. "lsl __tmp_reg__\n\t"
  301. "out %[port],__tmp_reg__\n\t"
  302. "delay2\n\t" //6
  303. "lsl __tmp_reg__\n\t"
  304. "out %[port],__tmp_reg__\n\t"
  305. "delay1\n\t" //7
  306. "lsl __tmp_reg__\n\t"
  307. "dec %[hres]\n\t"
  308. "out %[port],__tmp_reg__\n\t"
  309. "brne loop4\n\t" //go too loop4
  310. "delay1\n\t" //8
  311. "lsl __tmp_reg__\n\t"
  312. "out %[port],__tmp_reg__\n\t"
  313. "delay3\n\t"
  314. "cbi %[port],7\n\t"
  315. :
  316. : [port] "i" (_SFR_IO_ADDR(PORT_VID)),
  317. "x" (display.screen),
  318. "y" (renderLine),
  319. [hres] "d" (display.hres)
  320. : "r16" // try to remove this clobber later...
  321. );
  322. #endif
  323. }
  324. // only 16mhz right now!!!
  325. void render_line3c() {
  326. #ifndef REMOVE3C
  327. __asm__ __volatile__ (
  328. ".macro byteshift\n\t"
  329. "LD __tmp_reg__,X+\n\t"
  330. "out %[port],__tmp_reg__\n\t" //0
  331. "nop\n\t"
  332. "lsl __tmp_reg__\n\t"
  333. "out %[port],__tmp_reg__\n\t" //1
  334. "nop\n\t"
  335. "lsl __tmp_reg__\n\t"
  336. "out %[port],__tmp_reg__\n\t" //2
  337. "nop\n\t"
  338. "lsl __tmp_reg__\n\t"
  339. "out %[port],__tmp_reg__\n\t" //3
  340. "nop\n\t"
  341. "lsl __tmp_reg__\n\t"
  342. "out %[port],__tmp_reg__\n\t" //4
  343. "nop\n\t"
  344. "lsl __tmp_reg__\n\t"
  345. "out %[port],__tmp_reg__\n\t" //5
  346. "nop\n\t"
  347. "lsl __tmp_reg__\n\t"
  348. "out %[port],__tmp_reg__\n\t" //6
  349. "nop\n\t"
  350. "lsl __tmp_reg__\n\t"
  351. "out %[port],__tmp_reg__\n\t" //7
  352. ".endm\n\t"
  353. "ADD r26,r28\n\t"
  354. "ADC r27,r29\n\t"
  355. "cpi %[hres],30\n\t" //615
  356. "breq skip0\n\t"
  357. "cpi %[hres],29\n\t"
  358. "breq jumpto1\n\t"
  359. "cpi %[hres],28\n\t"
  360. "breq jumpto2\n\t"
  361. "cpi %[hres],27\n\t"
  362. "breq jumpto3\n\t"
  363. "cpi %[hres],26\n\t"
  364. "breq jumpto4\n\t"
  365. "cpi %[hres],25\n\t"
  366. "breq jumpto5\n\t"
  367. "cpi %[hres],24\n\t"
  368. "breq jumpto6\n\t"
  369. "jumpto1:\n\t"
  370. "rjmp skip1\n\t"
  371. "jumpto2:\n\t"
  372. "rjmp skip2\n\t"
  373. "jumpto3:\n\t"
  374. "rjmp skip3\n\t"
  375. "jumpto4:\n\t"
  376. "rjmp skip4\n\t"
  377. "jumpto5:\n\t"
  378. "rjmp skip5\n\t"
  379. "jumpto6:\n\t"
  380. "rjmp skip6\n\t"
  381. "skip0:\n\t"
  382. "byteshift\n\t" //1 \\643
  383. "skip1:\n\t"
  384. "byteshift\n\t" //2
  385. "skip2:\n\t"
  386. "byteshift\n\t" //3
  387. "skip3:\n\t"
  388. "byteshift\n\t" //4
  389. "skip4:\n\t"
  390. "byteshift\n\t" //5
  391. "skip5:\n\t"
  392. "byteshift\n\t" //6
  393. "skip6:\n\t"
  394. "byteshift\n\t" //7
  395. "byteshift\n\t" //8
  396. "byteshift\n\t" //9
  397. "byteshift\n\t" //10
  398. "byteshift\n\t" //11
  399. "byteshift\n\t" //12
  400. "byteshift\n\t" //13
  401. "byteshift\n\t" //14
  402. "byteshift\n\t" //15
  403. "byteshift\n\t" //16
  404. "byteshift\n\t" //17
  405. "byteshift\n\t" //18
  406. "byteshift\n\t" //19
  407. "byteshift\n\t" //20
  408. "byteshift\n\t" //21
  409. "byteshift\n\t" //22
  410. "byteshift\n\t" //23
  411. "byteshift\n\t" //24
  412. "byteshift\n\t" //25
  413. "byteshift\n\t" //26
  414. "byteshift\n\t" //27
  415. "byteshift\n\t" //28
  416. "byteshift\n\t" //29
  417. "byteshift\n\t" //30
  418. "delay2\n\t"
  419. "cbi %[port],7\n\t"
  420. :
  421. : [port] "i" (_SFR_IO_ADDR(PORT_VID)),
  422. "x" (display.screen),
  423. "y" (renderLine),
  424. [hres] "d" (display.hres)
  425. : "r16" // try to remove this clobber later...
  426. );
  427. #endif
  428. }