; ------------------------------------------------- ; 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 addr_msb_max: equ 0x40 ; 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 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 round: db 1 ; ------------------------------------------------- ; Program ; ------------------------------------------------- code reset: org 0x000 jmp main ; ------------------------------------------------- ; ISR ; ------------------------------------------------- org 0x001 reti ; ------------------------------------------------- ; Main ; ------------------------------------------------- main: mov R0, 0x00 xout (led_port), R0 ; xin R0, (btn_port) ; tst R0 ; jz no_vga mov R0, CTRL_VGA_ENABLE xout (ctrl_reg), R0 no_vga: mov R0, 0 mov R1, 0x80 xout (reg_color_red), R0 xout (reg_color_grn), R1 xout (reg_color_blu), R0 jmp do_write ; ---------------------------------------------------------- ; SDRAM clear ; ---------------------------------------------------------- ; Set SDRAM address register to 0 do_clear: 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: xin R0, (sdram_addr+0) cmp R0, addr_msb_max jeq clear_finished 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 ; Advance address counter mov R0, BURST_LEN call AddrCnt_Adv jmp sdr_clear clear_finished: call AddrCnt_Disp ; ---------------------------------------------------------- ; SDRAM Write ; ---------------------------------------------------------- do_write: ; Clear LCD ; Set SDRAM address register to 0 call AddrCnt_Init mov R0, 0 movc (round), R0 sdr_write: xin R0, (sdram_addr+0) cmp R0, addr_msb_max jeq write_finished mov R2, BURST_LEN ; Fill SDR Buffer sdr_burst_wr: movc R1, (round) ; xin R0, (sdram_addr+2) ; cmp R0, 0x7F ; jne force_no_err ; inc R1 force_no_err: 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, (round) inc R0 movc (round), R0 sub R2, 2 jnz sdr_burst_wr ; Start SDR write call wait_cmd_rdy mov R0, SD_CMD_WRITE xout (sdram_reg), R0 ; Advance address counter mov R0, BURST_LEN call AddrCnt_Adv jmp sdr_write write_finished: call AddrCnt_Disp ; ---------------------------------------------------------- ; SDRAM Verify ; ---------------------------------------------------------- do_read: ; Clear LCD ; Set SDRAM address register to 0 call AddrCnt_Init mov R0, 0 movc (round), R0 sdr_read: xin R0, (sdram_addr+2) cmp R0, addr_msb_max jeq read_finished ; 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, (round) 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, (round) inc R0 movc (round), R0 sub R3, 2 jnz poll_read_fin ; Advance address counter mov R0, BURST_LEN call AddrCnt_Adv jmp sdr_read ; ---------------------------------------------------------- sd_request: push R0 mov R0, SDRAM_CTRL_REQUEST xout (sdram_reg), R0 wait4grant: xin R0, (sdram_reg) xout (led_port), R0 and R0, SDRAM_STAT_ACCESS jz wait4grant pop R0 ret ; ---------------------------------------------------------- wait_cmd_rdy: call sd_request 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: jmp ende verify_error: jmp ende ende: jmp do_clear ; ------------------------------------------------- AddrCnt_Init: push R0 mov R0, 0x00 xout (sdram_addr+0), R0 xout (sdram_addr+1), R0 xout (sdram_addr+2), R0 pop R0 ret ; ------------------------------------------------- ; Parameter ; R0: increment value AddrCnt_Adv: 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 ; ------------------------------------------------- ; Display Address counter AddrCnt_Disp: ret ; ------------------------------------------------- ; PutHexByte ; R0 : Byte value PutHexByte: ret ; ------------------------------------------------- print_addr: ret ; ------------------------------------------------- print_data: ret ; ------------------------------------------------- include "../../../lib/jasm/utils.inc.jsm"