代码比较简单,大家看看就明白了。
#include "bin2ldr.h"
#define hi(x) (x >> 16)
#define lo(x) (x & 0xffff)
/* UART Controller (0xFFC00400 - 0xFFC004FF) */
/* Transmit Holding register */
#define UART_THR 0xFFC00400
/* Receive Buffer register */
#define UART_RBR 0xFFC00400
/* Divisor Latch (Low-Byte) */
#define UART_DLL 0xFFC00400
/* Interrupt Enable Register */
#define UART_IER 0xFFC00404
/* Divisor Latch (High-Byte) */
#define UART_DLH 0xFFC00404
/* Interrupt Identification Register */
#define UART_IIR 0xFFC00408
/* Line Control Register */
#define UART_LCR 0xFFC0040C
/* Modem Control Register */
#define UART_MCR 0xFFC00410
/* Line Status Register */
#define UART_LSR 0xFFC00414
/* SCR Scratch Register */
#define UART_SCR 0xFFC0041C
/* Global Control Register */
#define UART_GCTL 0xFFC00424
/* Initialise UART */
p0.h = hi(UART_LCR);
p0.l = lo(UART_LCR);
/* 8-bit word; 1 stop bit; No parity */
r0 = 0x83(Z);
w[p0] = r0.l; /* To enable DLL writes */
ssync;
p0.h = hi(UART_DLL);
p0.l = lo(UART_DLL);
/* SCLK = 133MHz, BaudRate=115200
BaudRate = SCLK /(16 * Divisor) */
r0 = 0x48(Z);
w[p0] = r0.l;
ssync;
p0.h = hi(UART_DLH);
p0.l = lo(UART_DLH);
r0 = 0x00(Z);
w[p0] = r0.l;
ssync;
p0.h = hi(UART_GCTL);
p0.l = lo(UART_GCTL);
r0 = 0x1(Z);
w[p0] = r0.l; /* To enable UART clock */
ssync;
/* Begin to process rx/tx data */
p0.h = hi(UART_LCR);
p0.l = lo(UART_LCR);
/* 8-bit word; 1 stop bit; No parity */
r0 = 0x03(Z);
/* To enable access THR/PBR */
w[p0] = r0.l;
ssync;
/* Write 'Hello Bfincn' to Console */
p0.h = hi(UART_THR);
p0.l = lo(UART_THR);
r0 = 0x48(Z); /* H */
w[p0] = r0.l;
ssync;
ww1:
p0.h = hi(UART_LSR);
p0.l = lo(UART_LSR);
r0 = w[p0](z);
cc = bittst(r0,5);
if !cc jump ww1;
p0.h = hi(UART_THR);
p0.l = lo(UART_THR);
r0 = 0x65(Z); /* e */
w[p0] = r0.l;
ssync;
ww2:
p0.h = hi(UART_LSR);
p0.l = lo(UART_LSR);
r0 = w[p0](z);
cc = bittst(r0,5);
if !cc jump ww2;
p0.h = hi(UART_THR);
p0.l = lo(UART_THR);
r0 = 0x6c(Z); /* l */
w[p0] = r0.l;
ssync;
ww3:
p0.h = hi(UART_LSR);
p0.l = lo(UART_LSR);
r0 = w[p0](z);
cc = bittst(r0,5);
if !cc jump ww3;
p0.h = hi(UART_THR);
p0.l = lo(UART_THR);
r0 = 0x6c(Z); /* l */
w[p0] = r0.l;
ssync;
ww4:
p0.h = hi(UART_LSR);
p0.l = lo(UART_LSR);
r0 = w[p0](z);
cc = bittst(r0,5);
if !cc jump ww4;
p0.h = hi(UART_THR);
p0.l = lo(UART_THR);
r0 = 0x6f(Z); /* l */
w[p0] = r0.l;
ssync;
ww5:
p0.h = hi(UART_LSR);
p0.l = lo(UART_LSR);
r0 = w[p0](z);
cc = bittst(r0,5);
if !cc jump ww5;
p0.h = hi(UART_THR);
p0.l = lo(UART_THR);
r0 = 0x20(Z); /* space */
w[p0] = r0.l;
ssync;
ww6:
p0.h = hi(UART_LSR);
p0.l = lo(UART_LSR);
r0 = w[p0](z);
cc = bittst(r0,5);
if !cc jump ww6;
p0.h = hi(UART_THR);
p0.l = lo(UART_THR);
r0 = 0x42(Z); /* B */
w[p0] = r0.l;
ssync;
ww7:
p0.h = hi(UART_LSR);
p0.l = lo(UART_LSR);
r0 = w[p0](z);
cc = bittst(r0,5);
if !cc jump ww7;
p0.h = hi(UART_THR);
p0.l = lo(UART_THR);
r0 = 0x66(Z); /* f */
w[p0] = r0.l;
ssync;
ww8:
p0.h = hi(UART_LSR);
p0.l = lo(UART_LSR);
r0 = w[p0](z);
cc = bittst(r0,5);
if !cc jump ww8;
p0.h = hi(UART_THR);
p0.l = lo(UART_THR);
r0 = 0x69(Z); /* i */
w[p0] = r0.l;
ssync;
ww9:
p0.h = hi(UART_LSR);
p0.l = lo(UART_LSR);
r0 = w[p0](z);
cc = bittst(r0,5);
if !cc jump ww9;
p0.h = hi(UART_THR);
p0.l = lo(UART_THR);
r0 = 0x6e(Z); /* n */
w[p0] = r0.l;
ssync;
ww10:
p0.h = hi(UART_LSR);
p0.l = lo(UART_LSR);
r0 = w[p0](z);
cc = bittst(r0,5);
if !cc jump ww10;
p0.h = hi(UART_THR);
p0.l = lo(UART_THR);
r0 = 0x63(Z); /* c */
w[p0] = r0.l;
ssync;
ww11:
p0.h = hi(UART_LSR);
p0.l = lo(UART_LSR);
r0 = w[p0](z);
cc = bittst(r0,5);
if !cc jump ww11;
p0.h = hi(UART_THR);
p0.l = lo(UART_THR);
r0 = 0x6e(Z); /* n */
w[p0] = r0.l;
ssync;
ww12:
p0.h = hi(UART_LSR);
p0.l = lo(UART_LSR);
r0 = w[p0](z);
cc = bittst(r0,0);
if !cc jump ww12;
/* read from buffer */
p0.h = hi(UART_RBR);
p0.l = lo(UART_RBR);
r1 = w[p0](z);
/* send buffer is empty or not */
wait:
p0.h = hi(UART_LSR);
p0.l = lo(UART_LSR);
r0 = w[p0](z);
cc = bittst(r0,5);
if !cc jump wait;
/* write back to console */
p0.h = hi(UART_THR);
p0.l = lo(UART_THR);
w[p0] = r1.l;
ssync;
/* if user enter the char 'q' then exit */
r1 = 0x72(z);
cc = r0 == r1; /* q */
if !cc jump ww12;
p0.l = (APP_ENTRY & 0xFFFF);
p0.h = (APP_ENTRY >> 16);
jump (p0);
没有评论:
发表评论