; ------------------------------------------------- ; Register definitions ; ------------------------------------------------- include "../../../lib/jasm/cregs.inc.jsm" include "xregs.inc.jsm" ; ------------------------------------------------- ; Constants ; ------------------------------------------------- BURST_LEN: equ 8 SD_CMD_WRITE: equ 0x06 SD_CMD_READ: equ 0x05 START_ADDR0: equ 0x00 START_ADDR1: equ 0x00 START_ADDR2: equ 0x20 END_ADDR0: equ 0xF8 END_ADDR1: equ 0xFF END_ADDR2: equ 0xFF SIO_TIMEOUT0: equ 0xE8 SIO_TIMEOUT1: equ 0x03 SIO_BUFSIZE: equ 64 ; External RAM/ROM xmem crlf: dc 13, 10, 0 greet_txt: dc "SDRAM Test", 0 msg_txt0: dc "Clearing SDRAM", 0 msg_txt1: dc "Writing SDRAM", 0 msg_txt2: dc "Reading SDRAM", 0 msg_txt3: dc "Reading Bitmap", 0 verify_ok: dc "Verify OK", 0 verify_nok: dc "Failed at ", 0 dot: dc ".", 0 org 0x80 sdram_data: db 8 ; SDRAM data window at X:0x80 ; Internal RAM cmem 0 block_cnt: db 1 round_cnt: db 1 bmp_offset: db 4 pixel_buf: db 24 sio_rx_buf: db 64 sio_rx_ptr_r: db 1 sio_rx_ptr_w: db 1 sio_timeout: db 2 ; ------------------------------------------------- ; Program ; ------------------------------------------------- code reset: org 0x000 jmp main ; ------------------------------------------------- ; ISR ; ------------------------------------------------- org 0x001 sio_isr: push R0 push R1 u_get_nxt: xin R1, (uart_status) and R1, 0x10 jz ex_uart_isr xin R0, (uart_data) movc R1, (sio_rx_ptr_w) movc (R1), R0 inc R1 cmp R1, sio_rx_buf+64 jne u_upd_nxt mov R1, sio_rx_buf u_upd_nxt: movc (sio_rx_ptr_w), R1 movc R0, (sio_rx_ptr_r) cmp R0, R1 jne u_get_nxt xin R0, (ctrl_reg) or R0, 0x20 xout (ctrl_reg), R0 jmp u_get_nxt ex_uart_isr: call Timer_isr pop R1 pop R0 reti Timer_isr: xin R0, (btn_port) and R0, 0x01 jz btn_chk2 mov R0, vga_read_addr xout (R0), 0x00 inc R0 xout (R0), 0x00 inc R0 xout (R0), 0x00 jmp ex_t_isr btn_chk2: xin R0, (btn_port) and R0, 0x02 jz ex_t_isr mov R0, vga_read_addr xout (R0), 0x00 inc R0 xout (R0), 0x00 inc R0 xout (R0), 0x20 ex_t_isr: ret ; ------------------------------------------------- ; Main ; ------------------------------------------------- main: call sio_init call LCD_init mov R0, 0x00 xout (led_port), R0 mov R0, CTRL_VGA_ENABLE xout (ctrl_reg), R0 xin R0, (ctrl_reg) or R0, CTRL_TIMER_ENABLE xout (ctrl_reg), R0 mov R0, 0x03 cout (cpu_int_ctrl), R0 ; Clear LCD mov R0, LCD_CMD_CLEAR call LCD_writecmd mov R0, 0 mov R1, 0xC0 xout (reg_color_red), R1 xout (reg_color_grn), R1 xout (reg_color_blu), R1 call cg_wait_rdy call cg_clr_screen ; Print greeting message mov R1, greet_txt call LCD_putsx mov R1, greet_txt call cg_puts mov R1, crlf call cg_puts_clr xin R0, (ctrl_reg) and R0, 0xCF xout (ctrl_reg), R0 mov R0, 0 movc (round_cnt), R0 main_loop: call write_uart_data ; call do_clear ML1: call do_write call do_read movc R0, (round_cnt) inc R0 movc (round_cnt), R0 jmp ML1 ; ---------------------------------------------------------- ; SDRAM clear ; ---------------------------------------------------------- do_clear: ; Clear LCD mov R0, LCD_CMD_CLEAR call LCD_writecmd ; Print Clear message mov R1, msg_txt0 call LCD_putsx mov R1, msg_txt0 call cg_clr_puts mov R1, crlf call cg_puts_clr ; Set SDRAM address register to 0 mov R0, 0 mov R1, 0 mov R2, 0 call AddrCnt_Init ; Fill SDR Buffer with zeros mov R0, sdram_data mov R15, 8 buf_clr_loop: movx (R0), 0 inc R0 dec R15 jnz buf_clr_loop sdr_clear: mov R2, BURST_LEN poll_clear_full: xin R0, (sdram_reg) xout (led_port), R0 and R0, FIFO_STAT_WRITE_DATA_FULL jnz poll_clear_full ; Fill FIFO mov R0, FIFO_CTRL_IS_WRITE xout (sdram_reg), R0 sub R2, 2 jnz poll_clear_full ; Start SDR write call wait_cmd_rdy mov R0, SD_CMD_WRITE xout (sdram_reg), R0 xin R0, (sdram_addr+0) cmp R0, END_ADDR0 jne next_addr_c xin R0, (sdram_addr+1) cmp R0, END_ADDR1 jne next_addr_c xin R0, (sdram_addr+2) cmp R0, END_ADDR2 jeq clear_finished ; Advance address counter next_addr_c: mov R0, BURST_LEN call AddrCnt_Inc jmp sdr_clear clear_finished: call AddrCnt_Disp call delay1s ret ; ---------------------------------------------------------- ; SDRAM Write ; ---------------------------------------------------------- write_uart_data: ; Clear LCD mov R0, LCD_CMD_CLEAR call LCD_writecmd ; Print Clear message mov R1, msg_txt3 call LCD_putsx mov R1, msg_txt3 call cg_clr_puts mov R1, crlf call cg_puts_clr ; Set SDRAM address register to 0 ; mov R0, 0xF8 ; 800x600 ; mov R1, 0x52 ; 800x600 ; mov R2, 0x07 ; 800x600 mov R0, 0xF8 ; 1280x1024 mov R1, 0xFF ; 1280x1024 mov R2, 0x13 ; 1280x1024 call AddrCnt_Init read_bmp: mov R15, 0 skip_hdr1: call sio_getchar jc write_uart_data inc R15 cmp R15, 10 jne skip_hdr1 call sio_getchar jc write_uart_data inc R15 movc (bmp_offset+0), R0 call sio_getchar jc write_uart_data inc R15 movc (bmp_offset+1), R0 call sio_getchar jc write_uart_data inc R15 movc (bmp_offset+2), R0 call sio_getchar jc write_uart_data inc R15 movc (bmp_offset+3), R0 movc R1, (bmp_offset+0) skip_hdr2: call sio_getchar jc write_uart_data inc R15 cmp R15, R1 jne skip_hdr2 get_pixel: mov R15, 8 mov R1, pixel_buf+23 next_pixel: mov R2, 3 next_color: dec R2 mov R3, R1 call sio_getchar jc sdw_fin sub R3, R2 movc (R3), R0 tst R2 jnz next_color sub R1, 3 dec R15 jnz next_pixel mov R2, BURST_LEN ; Fill SDR Buffer mov R1, pixel_buf wls: mov R3, sdram_data wls0: movc R0, (R1) movx (R3), R0 inc R1 inc R3 cmp R3, sdram_data+6 jne wls0 pwf: xin R0, (sdram_reg) xout (led_port), R0 and R0, FIFO_STAT_WRITE_DATA_FULL jnz pwf ; Fill FIFO mov R0, FIFO_CTRL_IS_WRITE xout (sdram_reg), R0 sub R2, 2 jnz wls ; Start SDR write call wait_cmd_rdy mov R0, SD_CMD_WRITE xout (sdram_reg), R0 call AddrCnt_Disp xin R0, (sdram_addr+0) tst R0 jnz next_addr xin R0, (sdram_addr+1) tst R0 jnz next_addr xin R0, (sdram_addr+2) tst R0 jz sdw_fin ; Advance address counter next_addr: mov R0, BURST_LEN call AddrCnt_Dec jmp get_pixel sdw_fin: call delay1s ret ; ---------------------------------------------------------- ; SDRAM Write ; ---------------------------------------------------------- do_write: ; Clear LCD mov R0, LCD_CMD_CLEAR call LCD_writecmd ; Print Write message mov R1, msg_txt1 call LCD_putsx mov R1, msg_txt1 call cg_clr_puts mov R1, crlf call cg_puts_clr ; Set SDRAM address register to 0 mov R0, START_ADDR0 mov R1, START_ADDR1 mov R2, START_ADDR2 call AddrCnt_Init movc R0, (round_cnt) movc (block_cnt), R0 sdr_write: mov R2, BURST_LEN ; Fill SDR Buffer sdr_burst_wr: movc R1, (block_cnt) mov R0, sdram_data mov R15, 8 write_loop: movx (R0), R1 inc R0 inc R1 dec R15 jnz write_loop poll_write_full: xin R0, (sdram_reg) xout (led_port), R0 and R0, FIFO_STAT_WRITE_DATA_FULL jnz poll_write_full ; Fill FIFO mov R0, FIFO_CTRL_IS_WRITE xout (sdram_reg), R0 movc R0, (block_cnt) inc R0 movc (block_cnt), R0 sub R2, 2 jnz sdr_burst_wr ; Start SDR write call wait_cmd_rdy mov R0, SD_CMD_WRITE xout (sdram_reg), R0 xin R0, (sdram_addr+0) cmp R0, END_ADDR0 jne next_addr_w xin R0, (sdram_addr+1) cmp R0, END_ADDR1 jne next_addr_w xin R0, (sdram_addr+2) cmp R0, END_ADDR2 jeq write_finished ; Advance address counter next_addr_w: mov R0, BURST_LEN call AddrCnt_Inc jmp sdr_write write_finished: call AddrCnt_Disp call delay1s ret ; ---------------------------------------------------------- ; SDRAM Verify ; ---------------------------------------------------------- do_read: ; Clear LCD mov R0, LCD_CMD_CLEAR call LCD_writecmd ; Print Read message mov R1, msg_txt2 call LCD_putsx mov R1, msg_txt2 call cg_clr_puts mov R1, crlf call cg_puts_clr ; Set SDRAM address register to 0 mov R0, START_ADDR0 mov R1, START_ADDR1 mov R2, START_ADDR2 call AddrCnt_Init movc R0, (round_cnt) movc (block_cnt), R0 ; flush_data flush_data: xin R0, (sdram_reg) xout (led_port), R0 and R0, FIFO_STAT_READ_DATA_EMPTY jnz sdr_read mov R0, FIFO_CTRL_IS_READ xout (sdram_reg), R0 jmp flush_data sdr_read: ; Start SDR read call wait_cmd_rdy mov R0, SD_CMD_READ xout (sdram_reg), R0 mov R3, BURST_LEN ; Wait last read poll_read_fin: xin R0, (sdram_reg) xout (led_port), R0 and R0, FIFO_STAT_READ_DATA_EMPTY jnz poll_read_fin ; Verify SDR Buffer movc R1, (block_cnt) mov R0, sdram_data mov R15, 8 read_loop: movx R2, (R0) cmp R2, R1 jne verify_error inc R0 inc R1 dec R15 jnz read_loop ; Advance Read-FIFO mov R0, FIFO_CTRL_IS_READ xout (sdram_reg), R0 movc R0, (block_cnt) inc R0 movc (block_cnt), R0 sub R3, 2 jnz poll_read_fin xin R0, (sdram_addr+0) cmp R0, END_ADDR0 jne next_addr_r xin R0, (sdram_addr+1) cmp R0, END_ADDR1 jne next_addr_r xin R0, (sdram_addr+2) cmp R0, END_ADDR2 jeq read_finished ; Advance address counter next_addr_r: mov R0, BURST_LEN call AddrCnt_Inc jmp sdr_read ; ---------------------------------------------------------- wait_cmd_rdy: push R0 wait4cmd: xin R0, (sdram_reg) xout (led_port), R0 and R0, FIFO_STAT_CMD_FULL jnz wait4cmd pop R0 ret ; ---------------------------------------------------------- read_finished: no_error: mov R0, LCD_CMD_CLEAR call LCD_writecmd mov R1, verify_ok call LCD_putsx mov R0, LCD_CMD_Z2 call LCD_writecmd call print_addr mov R1, verify_ok call cg_clr_puts mov R1, crlf call cg_puts_clr jmp ende verify_error: mov R0, LCD_CMD_CLEAR call LCD_writecmd mov R1, verify_nok call LCD_putsx call print_addr call print_data mov R1, verify_nok call cg_clr_puts mov R1, crlf call cg_puts_clr xin R0, (ctrl_reg) or R0, 0x10 xout (ctrl_reg), R0 jmp ende ende: call delay1s ret ; ------------------------------------------------- AddrCnt_Init: xout (sdram_addr+0), R0 xout (sdram_addr+1), R1 xout (sdram_addr+2), R2 ret ; ------------------------------------------------- ; Parameter ; R0: increment value AddrCnt_Inc: push R1 push R2 push R3 xin R1, (sdram_addr+0) xin R2, (sdram_addr+1) xin R3, (sdram_addr+2) add R1, R0 addc R2, 0 addc R3, 0 xout (sdram_addr+0), R1 xout (sdram_addr+1), R2 xout (sdram_addr+2), R3 pop R3 pop R2 pop R1 ret ; ------------------------------------------------- ; Parameter ; R0: decrement value AddrCnt_Dec: push R1 push R2 push R3 xin R1, (sdram_addr+0) xin R2, (sdram_addr+1) xin R3, (sdram_addr+2) sub R1, R0 subc R2, 0 subc R3, 0 xout (sdram_addr+0), R1 xout (sdram_addr+1), R2 xout (sdram_addr+2), R3 pop R3 pop R2 pop R1 ret ; ------------------------------------------------- ; Display Address counter AddrCnt_Disp: mov R0, LCD_CMD_Z2 call LCD_writecmd call print_addr ret ; ------------------------------------------------- ; PutHexByte ; R0 : Byte value PutHexByte: push R1 call bin2hex call LCD_writechar mov R0, R1 call LCD_writechar pop R1 ret ; ------------------------------------------------- print_addr: push R0 xin R0, (sdram_addr+2) call PutHexByte xin R0, (sdram_addr+1) call PutHexByte xin R0, (sdram_addr+0) call PutHexByte pop R0 ret ; ------------------------------------------------- ; R1 : ptr to byte array print_dword: push R0 add R1, 3 movc R0, (R1) call PutHexByte dec R1 movc R0, (R1) call PutHexByte dec R1 movc R0, (R1) call PutHexByte dec R1 movc R0, (R1) call PutHexByte pop R0 ret ; ------------------------------------------------- print_data: push R0 mov R0, LCD_CMD_Z2 call LCD_writecmd movx R0, (sdram_data+7) call PutHexByte movx R0, (sdram_data+6) call PutHexByte movx R0, (sdram_data+5) call PutHexByte movx R0, (sdram_data+4) call PutHexByte movx R0, (sdram_data+3) call PutHexByte movx R0, (sdram_data+2) call PutHexByte movx R0, (sdram_data+1) call PutHexByte movx R0, (sdram_data+0) call PutHexByte pop R0 ret ; ------------------------------------------------- sio_init: push R0 mov R0, 13 xout (uart_baudrate), R0 mov R0, sio_rx_buf movc (sio_rx_ptr_r), R0 movc (sio_rx_ptr_w), R0 mov R0, SIO_TIMEOUT0 movc (sio_timeout+0), R0 mov R0, SIO_TIMEOUT1 movc (sio_timeout+1), R0 pop R0 ret ; ------------------------------------------------- ; Params: none ; Return: ; R0 : received character sio_getchar: push R1 push R2 push R3 movc R2, (sio_timeout+0) movc R3, (sio_timeout+1) movc R1, (sio_rx_ptr_r) sio_wait_rx: movc R0, (sio_rx_ptr_w) cmp R0, R1 jeq wait_rx_stub movc R0, (R1) inc R1 cmp R1, sio_rx_buf+64 jne u_upd_r_ptr mov R1, sio_rx_buf u_upd_r_ptr: movc (sio_rx_ptr_r), R1 clrc ex_sio_gc: pop R3 pop R2 pop R1 ret wait_rx_stub: call delay1ms dec R2 subc R3, 0 jnz sio_wait_rx tst R2 jnz sio_wait_rx sio_to: setc jmp ex_sio_gc ; ------------------------------------------------- include "../../../lib/jasm/utils.inc.jsm" include "../../../lib/jasm/lcd.inc.jsm" include "../../../lib/jasm/cg.inc.jsm" include "../../../lib/jasm/delays.inc.jsm" include "../../../lib/jasm/uart.inc.jsm"