+void mmap_init(u8 memtype) {
+
+ MEMMAP=memtype;
+}
+
+void pll_init(void) {
+
+ /* configuration */
+ PLLCFG=0x42; // multiplier = 3 (for cclk), dividor = 4 (for f_cco)
+ PLLCON=0x03; // enable and set as clk source for the lpc
+ /* feed sequence */
+ PLLFEED=0xaa;
+ PLLFEED=0x55;
+ /* wait for lock */
+ while(!(PLLSTAT&(1<<10)))
+ continue;
+}
+
+void ext_mem_bank_init(void) {
+
+ BCFG0=0x10000420; // flash 1
+ BCFG1=0x00000c42; // lcd
+ BCFG2=0x10000420; // flash 2
+}
+
+void pin_select_init() {
+
+ /*
+ * a[19:2] -> address lines
+ */
+
+ PINSEL2=0x0d6041d4;
+}
+
+void uart0_init(void) {
+
+ PINSEL0=0x05; // pin select -> tx, rx
+ UART0_FCR=0x07; // enable fifo
+ UART0_LCR=0x83; // set dlab + word length
+ UART0_DLL=0x04; // br: 38400 @ 10/4 mhz
+ UART0_DLM=0x00;
+ UART0_LCR=0x03; // unset dlab
+}
+
+void uart0_send_string(char *txbuf) {
+
+ int i;
+
+ i=0;
+
+ while(txbuf[i]) {
+ UART0_THR=txbuf[i++];
+ /* flush if tx buffer maximum reached */
+ if(!(i%16))
+ while(!(UART0_LSR&(1<<6)))
+ continue;
+ }
+
+ /* flush if \n and \r do not fit in the tx buffer */
+ if(i>14)
+ while(!(UART0_LSR&(1<<6)))
+ continue;
+
+ UART0_THR='\n';
+ UART0_THR='\r';
+
+ /* flush uart0 anyways */
+ while(!(UART0_LSR&(1<<6)))
+ continue;
+}
+
+void uart0_send_buf16(u16 *buf,int len) {
+
+ int i;
+
+ i=0;
+
+ for(i=0;i<len/2;i++) {
+ if(!(i%8))
+ while(!(UART0_LSR&(1<<6)))
+ continue;
+ UART0_THR=buf[i]&0xff;
+ UART0_THR=(buf[i]>>8)&0xff;
+ }
+}
+
+void uart0_send_buf32(u32 *buf,int len) {
+
+ int i;
+
+ i=0;
+
+ for(i=0;i<len/4;i++) {
+ if(!(i%4))
+ while(!(UART0_LSR&(1<<6)))
+ continue;
+ UART0_THR=buf[i]&0xff;
+ UART0_THR=(buf[i]>>8)&0xff;
+ UART0_THR=(buf[i]>>16)&0xff;
+ UART0_THR=(buf[i]>>24)&0xff;
+ }
+}
+
+void uart0_send_byte(u8 send) {
+
+ while(!(UART0_LSR&(1<<5)))
+ continue;
+
+ UART0_THR=send;
+}
+
+u8 uart0_get_byte(void) {
+
+ u8 rx;
+
+ while(!(UART0_LSR&(1<<0)))
+ continue;
+
+ rx=UART0_RBR;
+
+ return rx;
+}
+
+void flash_reset(u8 bank) {
+
+ if((bank!='0')&(bank!='2'))
+ return;
+
+ if(bank=='0')
+ B0F=0xf0;
+ else
+ B2F=0xf0;
+}
+
+void flash_sector_erase(u8 flash,u8 sector) {
+
+ u32 a18_12;
+ u32 base;
+
+ if(sector>18)
+ return;
+ a18_12=sector_address[sector]<<1;
+
+ if((flash!='0')&(flash!='2'))
+ return;
+
+ switch(flash) {
+ case '0':
+ base=0x80000000;
+ B0F555=0xaa;
+ B0F2AA=0x55;
+ B0F555=0x80;
+ B0F555=0xaa;
+ B0F2AA=0x55;
+ *((volatile u16 *)(base|a18_12))=0x30;
+ break;
+ case '2':
+ base=0x82000000;
+ B2F555=0xaa;
+ B2F2AA=0x55;
+ B2F555=0x80;
+ B2F555=0xaa;
+ B2F2AA=0x55;
+ *((volatile u16 *)(base|a18_12))=0x30;
+ break;
+ default:
+ return;
+ }
+
+ return;
+}
+
+void flash_chip_erase(u8 bank) {
+
+ u8 status;
+
+ if((bank!='0')&(bank!='2'))
+ return;
+
+ if(bank=='0') {
+ B0F555=0xaa;
+ B0F2AA=0x55;
+ B0F555=0x80;
+ B0F555=0xaa;
+ B0F2AA=0x55;
+ B0F555=0x10;
+ }
+ else {
+ B2F555=0xaa;
+ B2F2AA=0x55;
+ B2F555=0x80;
+ B2F555=0xaa;
+ B2F2AA=0x55;
+ B2F555=0x10;
+ }