From e5a2b294560da680d70429b5622e8722b8463820 Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Thu, 11 Apr 2019 15:53:08 +0800 Subject: [PATCH] sw: fix line endings Signed-off-by: Sean Cross --- sw/include/generated/csr.h | 1102 ++++++++++++++------------------- sw/include/generated/mem.h | 29 +- sw/include/system.h | 7 +- sw/ld/output_format.ld | 2 +- sw/ld/regions.ld | 8 +- sw/src/main.c | 246 ++++---- sw/src/usb-dev.c | 5 +- sw/third_party/div.S | 240 +++---- sw/third_party/libbase/libc.c | 232 +++---- sw/third_party/mul.S | 52 +- sw/third_party/printf.c | 534 ++++++++-------- 11 files changed, 1139 insertions(+), 1318 deletions(-) diff --git a/sw/include/generated/csr.h b/sw/include/generated/csr.h index 75c8b6f..ffd6a80 100644 --- a/sw/include/generated/csr.h +++ b/sw/include/generated/csr.h @@ -1,650 +1,452 @@ -#ifndef __GENERATED_CSR_H -#define __GENERATED_CSR_H -#include -#ifdef CSR_ACCESSORS_DEFINED -extern void csr_writeb(uint8_t value, uint32_t addr); -extern uint8_t csr_readb(uint32_t addr); -extern void csr_writew(uint16_t value, uint32_t addr); -extern uint16_t csr_readw(uint32_t addr); -extern void csr_writel(uint32_t value, uint32_t addr); -extern uint32_t csr_readl(uint32_t addr); -#else /* ! CSR_ACCESSORS_DEFINED */ -#include -#endif /* ! CSR_ACCESSORS_DEFINED */ - -/* ctrl */ -#define CSR_CTRL_BASE 0xe0000000 -#define CSR_CTRL_RESET_ADDR 0xe0000000 -#define CSR_CTRL_RESET_SIZE 1 -static inline unsigned char ctrl_reset_read(void) { - unsigned char r = csr_readl(0xe0000000); - return r; -} -static inline void ctrl_reset_write(unsigned char value) { - csr_writel(value, 0xe0000000); -} -#define CSR_CTRL_SCRATCH_ADDR 0xe0000004 -#define CSR_CTRL_SCRATCH_SIZE 4 -static inline unsigned int ctrl_scratch_read(void) { - unsigned int r = csr_readl(0xe0000004); - r <<= 8; - r |= csr_readl(0xe0000008); - r <<= 8; - r |= csr_readl(0xe000000c); - r <<= 8; - r |= csr_readl(0xe0000010); - return r; -} -static inline void ctrl_scratch_write(unsigned int value) { - csr_writel(value >> 24, 0xe0000004); - csr_writel(value >> 16, 0xe0000008); - csr_writel(value >> 8, 0xe000000c); - csr_writel(value, 0xe0000010); -} -#define CSR_CTRL_BUS_ERRORS_ADDR 0xe0000014 -#define CSR_CTRL_BUS_ERRORS_SIZE 4 -static inline unsigned int ctrl_bus_errors_read(void) { - unsigned int r = csr_readl(0xe0000014); - r <<= 8; - r |= csr_readl(0xe0000018); - r <<= 8; - r |= csr_readl(0xe000001c); - r <<= 8; - r |= csr_readl(0xe0000020); - return r; -} - -/* picorvspi */ -#define CSR_PICORVSPI_BASE 0xe0005000 -#define CSR_PICORVSPI_CFG1_ADDR 0xe0005000 -#define CSR_PICORVSPI_CFG1_SIZE 1 -static inline unsigned char picorvspi_cfg1_read(void) { - unsigned char r = csr_readl(0xe0005000); - return r; -} -static inline void picorvspi_cfg1_write(unsigned char value) { - csr_writel(value, 0xe0005000); -} -#define CSR_PICORVSPI_CFG2_ADDR 0xe0005004 -#define CSR_PICORVSPI_CFG2_SIZE 1 -static inline unsigned char picorvspi_cfg2_read(void) { - unsigned char r = csr_readl(0xe0005004); - return r; -} -static inline void picorvspi_cfg2_write(unsigned char value) { - csr_writel(value, 0xe0005004); -} -#define CSR_PICORVSPI_CFG3_ADDR 0xe0005008 -#define CSR_PICORVSPI_CFG3_SIZE 1 -static inline unsigned char picorvspi_cfg3_read(void) { - unsigned char r = csr_readl(0xe0005008); - return r; -} -static inline void picorvspi_cfg3_write(unsigned char value) { - csr_writel(value, 0xe0005008); -} -#define CSR_PICORVSPI_CFG4_ADDR 0xe000500c -#define CSR_PICORVSPI_CFG4_SIZE 1 -static inline unsigned char picorvspi_cfg4_read(void) { - unsigned char r = csr_readl(0xe000500c); - return r; -} -static inline void picorvspi_cfg4_write(unsigned char value) { - csr_writel(value, 0xe000500c); -} -#define CSR_PICORVSPI_STAT1_ADDR 0xe0005010 -#define CSR_PICORVSPI_STAT1_SIZE 1 -static inline unsigned char picorvspi_stat1_read(void) { - unsigned char r = csr_readl(0xe0005010); - return r; -} -#define CSR_PICORVSPI_STAT2_ADDR 0xe0005014 -#define CSR_PICORVSPI_STAT2_SIZE 1 -static inline unsigned char picorvspi_stat2_read(void) { - unsigned char r = csr_readl(0xe0005014); - return r; -} -#define CSR_PICORVSPI_STAT3_ADDR 0xe0005018 -#define CSR_PICORVSPI_STAT3_SIZE 1 -static inline unsigned char picorvspi_stat3_read(void) { - unsigned char r = csr_readl(0xe0005018); - return r; -} -#define CSR_PICORVSPI_STAT4_ADDR 0xe000501c -#define CSR_PICORVSPI_STAT4_SIZE 1 -static inline unsigned char picorvspi_stat4_read(void) { - unsigned char r = csr_readl(0xe000501c); - return r; -} - -/* reboot */ -#define CSR_REBOOT_BASE 0xe0005800 -#define CSR_REBOOT_CTRL_ADDR 0xe0005800 -#define CSR_REBOOT_CTRL_SIZE 1 -static inline unsigned char reboot_ctrl_read(void) { - unsigned char r = csr_readl(0xe0005800); - return r; -} -static inline void reboot_ctrl_write(unsigned char value) { - csr_writel(value, 0xe0005800); -} - -/* rgb */ -#define CSR_RGB_BASE 0xe0006000 -#define CSR_RGB_DAT_ADDR 0xe0006000 -#define CSR_RGB_DAT_SIZE 1 -static inline unsigned char rgb_dat_read(void) { - unsigned char r = csr_readl(0xe0006000); - return r; -} -static inline void rgb_dat_write(unsigned char value) { - csr_writel(value, 0xe0006000); -} -#define CSR_RGB_ADDR_ADDR 0xe0006004 -#define CSR_RGB_ADDR_SIZE 1 -static inline unsigned char rgb_addr_read(void) { - unsigned char r = csr_readl(0xe0006004); - return r; -} -static inline void rgb_addr_write(unsigned char value) { - csr_writel(value, 0xe0006004); -} -#define CSR_RGB_CTRL_ADDR 0xe0006008 -#define CSR_RGB_CTRL_SIZE 1 -static inline unsigned char rgb_ctrl_read(void) { - unsigned char r = csr_readl(0xe0006008); - return r; -} -static inline void rgb_ctrl_write(unsigned char value) { - csr_writel(value, 0xe0006008); -} - -/* timer0 */ -#define CSR_TIMER0_BASE 0xe0002800 -#define CSR_TIMER0_LOAD_ADDR 0xe0002800 -#define CSR_TIMER0_LOAD_SIZE 4 -static inline unsigned int timer0_load_read(void) { - unsigned int r = csr_readl(0xe0002800); - r <<= 8; - r |= csr_readl(0xe0002804); - r <<= 8; - r |= csr_readl(0xe0002808); - r <<= 8; - r |= csr_readl(0xe000280c); - return r; -} -static inline void timer0_load_write(unsigned int value) { - csr_writel(value >> 24, 0xe0002800); - csr_writel(value >> 16, 0xe0002804); - csr_writel(value >> 8, 0xe0002808); - csr_writel(value, 0xe000280c); -} -#define CSR_TIMER0_RELOAD_ADDR 0xe0002810 -#define CSR_TIMER0_RELOAD_SIZE 4 -static inline unsigned int timer0_reload_read(void) { - unsigned int r = csr_readl(0xe0002810); - r <<= 8; - r |= csr_readl(0xe0002814); - r <<= 8; - r |= csr_readl(0xe0002818); - r <<= 8; - r |= csr_readl(0xe000281c); - return r; -} -static inline void timer0_reload_write(unsigned int value) { - csr_writel(value >> 24, 0xe0002810); - csr_writel(value >> 16, 0xe0002814); - csr_writel(value >> 8, 0xe0002818); - csr_writel(value, 0xe000281c); -} -#define CSR_TIMER0_EN_ADDR 0xe0002820 -#define CSR_TIMER0_EN_SIZE 1 -static inline unsigned char timer0_en_read(void) { - unsigned char r = csr_readl(0xe0002820); - return r; -} -static inline void timer0_en_write(unsigned char value) { - csr_writel(value, 0xe0002820); -} -#define CSR_TIMER0_UPDATE_VALUE_ADDR 0xe0002824 -#define CSR_TIMER0_UPDATE_VALUE_SIZE 1 -static inline unsigned char timer0_update_value_read(void) { - unsigned char r = csr_readl(0xe0002824); - return r; -} -static inline void timer0_update_value_write(unsigned char value) { - csr_writel(value, 0xe0002824); -} -#define CSR_TIMER0_VALUE_ADDR 0xe0002828 -#define CSR_TIMER0_VALUE_SIZE 4 -static inline unsigned int timer0_value_read(void) { - unsigned int r = csr_readl(0xe0002828); - r <<= 8; - r |= csr_readl(0xe000282c); - r <<= 8; - r |= csr_readl(0xe0002830); - r <<= 8; - r |= csr_readl(0xe0002834); - return r; -} -#define CSR_TIMER0_EV_STATUS_ADDR 0xe0002838 -#define CSR_TIMER0_EV_STATUS_SIZE 1 -static inline unsigned char timer0_ev_status_read(void) { - unsigned char r = csr_readl(0xe0002838); - return r; -} -static inline void timer0_ev_status_write(unsigned char value) { - csr_writel(value, 0xe0002838); -} -#define CSR_TIMER0_EV_PENDING_ADDR 0xe000283c -#define CSR_TIMER0_EV_PENDING_SIZE 1 -static inline unsigned char timer0_ev_pending_read(void) { - unsigned char r = csr_readl(0xe000283c); - return r; -} -static inline void timer0_ev_pending_write(unsigned char value) { - csr_writel(value, 0xe000283c); -} -#define CSR_TIMER0_EV_ENABLE_ADDR 0xe0002840 -#define CSR_TIMER0_EV_ENABLE_SIZE 1 -static inline unsigned char timer0_ev_enable_read(void) { - unsigned char r = csr_readl(0xe0002840); - return r; -} -static inline void timer0_ev_enable_write(unsigned char value) { - csr_writel(value, 0xe0002840); -} - -/* usb */ -#define CSR_USB_BASE 0xe0004800 -#define CSR_USB_PULLUP_OUT_ADDR 0xe0004800 -#define CSR_USB_PULLUP_OUT_SIZE 1 -static inline unsigned char usb_pullup_out_read(void) { - unsigned char r = csr_readl(0xe0004800); - return r; -} -static inline void usb_pullup_out_write(unsigned char value) { - csr_writel(value, 0xe0004800); -} -#define CSR_USB_EP_0_OUT_EV_STATUS_ADDR 0xe0004804 -#define CSR_USB_EP_0_OUT_EV_STATUS_SIZE 1 -static inline unsigned char usb_ep_0_out_ev_status_read(void) { - unsigned char r = csr_readl(0xe0004804); - return r; -} -static inline void usb_ep_0_out_ev_status_write(unsigned char value) { - csr_writel(value, 0xe0004804); -} -#define CSR_USB_EP_0_OUT_EV_PENDING_ADDR 0xe0004808 -#define CSR_USB_EP_0_OUT_EV_PENDING_SIZE 1 -static inline unsigned char usb_ep_0_out_ev_pending_read(void) { - unsigned char r = csr_readl(0xe0004808); - return r; -} -static inline void usb_ep_0_out_ev_pending_write(unsigned char value) { - csr_writel(value, 0xe0004808); -} -#define CSR_USB_EP_0_OUT_EV_ENABLE_ADDR 0xe000480c -#define CSR_USB_EP_0_OUT_EV_ENABLE_SIZE 1 -static inline unsigned char usb_ep_0_out_ev_enable_read(void) { - unsigned char r = csr_readl(0xe000480c); - return r; -} -static inline void usb_ep_0_out_ev_enable_write(unsigned char value) { - csr_writel(value, 0xe000480c); -} -#define CSR_USB_EP_0_OUT_LAST_TOK_ADDR 0xe0004810 -#define CSR_USB_EP_0_OUT_LAST_TOK_SIZE 1 -static inline unsigned char usb_ep_0_out_last_tok_read(void) { - unsigned char r = csr_readl(0xe0004810); - return r; -} -#define CSR_USB_EP_0_OUT_RESPOND_ADDR 0xe0004814 -#define CSR_USB_EP_0_OUT_RESPOND_SIZE 1 -static inline unsigned char usb_ep_0_out_respond_read(void) { - unsigned char r = csr_readl(0xe0004814); - return r; -} -static inline void usb_ep_0_out_respond_write(unsigned char value) { - csr_writel(value, 0xe0004814); -} -#define CSR_USB_EP_0_OUT_DTB_ADDR 0xe0004818 -#define CSR_USB_EP_0_OUT_DTB_SIZE 1 -static inline unsigned char usb_ep_0_out_dtb_read(void) { - unsigned char r = csr_readl(0xe0004818); - return r; -} -static inline void usb_ep_0_out_dtb_write(unsigned char value) { - csr_writel(value, 0xe0004818); -} -#define CSR_USB_EP_0_OUT_OBUF_HEAD_ADDR 0xe000481c -#define CSR_USB_EP_0_OUT_OBUF_HEAD_SIZE 1 -static inline unsigned char usb_ep_0_out_obuf_head_read(void) { - unsigned char r = csr_readl(0xe000481c); - return r; -} -static inline void usb_ep_0_out_obuf_head_write(unsigned char value) { - csr_writel(value, 0xe000481c); -} -#define CSR_USB_EP_0_OUT_OBUF_EMPTY_ADDR 0xe0004820 -#define CSR_USB_EP_0_OUT_OBUF_EMPTY_SIZE 1 -static inline unsigned char usb_ep_0_out_obuf_empty_read(void) { - unsigned char r = csr_readl(0xe0004820); - return r; -} -#define CSR_USB_EP_0_IN_EV_STATUS_ADDR 0xe0004824 -#define CSR_USB_EP_0_IN_EV_STATUS_SIZE 1 -static inline unsigned char usb_ep_0_in_ev_status_read(void) { - unsigned char r = csr_readl(0xe0004824); - return r; -} -static inline void usb_ep_0_in_ev_status_write(unsigned char value) { - csr_writel(value, 0xe0004824); -} -#define CSR_USB_EP_0_IN_EV_PENDING_ADDR 0xe0004828 -#define CSR_USB_EP_0_IN_EV_PENDING_SIZE 1 -static inline unsigned char usb_ep_0_in_ev_pending_read(void) { - unsigned char r = csr_readl(0xe0004828); - return r; -} -static inline void usb_ep_0_in_ev_pending_write(unsigned char value) { - csr_writel(value, 0xe0004828); -} -#define CSR_USB_EP_0_IN_EV_ENABLE_ADDR 0xe000482c -#define CSR_USB_EP_0_IN_EV_ENABLE_SIZE 1 -static inline unsigned char usb_ep_0_in_ev_enable_read(void) { - unsigned char r = csr_readl(0xe000482c); - return r; -} -static inline void usb_ep_0_in_ev_enable_write(unsigned char value) { - csr_writel(value, 0xe000482c); -} -#define CSR_USB_EP_0_IN_LAST_TOK_ADDR 0xe0004830 -#define CSR_USB_EP_0_IN_LAST_TOK_SIZE 1 -static inline unsigned char usb_ep_0_in_last_tok_read(void) { - unsigned char r = csr_readl(0xe0004830); - return r; -} -#define CSR_USB_EP_0_IN_RESPOND_ADDR 0xe0004834 -#define CSR_USB_EP_0_IN_RESPOND_SIZE 1 -static inline unsigned char usb_ep_0_in_respond_read(void) { - unsigned char r = csr_readl(0xe0004834); - return r; -} -static inline void usb_ep_0_in_respond_write(unsigned char value) { - csr_writel(value, 0xe0004834); -} -#define CSR_USB_EP_0_IN_DTB_ADDR 0xe0004838 -#define CSR_USB_EP_0_IN_DTB_SIZE 1 -static inline unsigned char usb_ep_0_in_dtb_read(void) { - unsigned char r = csr_readl(0xe0004838); - return r; -} -static inline void usb_ep_0_in_dtb_write(unsigned char value) { - csr_writel(value, 0xe0004838); -} -#define CSR_USB_EP_0_IN_IBUF_HEAD_ADDR 0xe000483c -#define CSR_USB_EP_0_IN_IBUF_HEAD_SIZE 1 -static inline unsigned char usb_ep_0_in_ibuf_head_read(void) { - unsigned char r = csr_readl(0xe000483c); - return r; -} -static inline void usb_ep_0_in_ibuf_head_write(unsigned char value) { - csr_writel(value, 0xe000483c); -} -#define CSR_USB_EP_0_IN_IBUF_EMPTY_ADDR 0xe0004840 -#define CSR_USB_EP_0_IN_IBUF_EMPTY_SIZE 1 -static inline unsigned char usb_ep_0_in_ibuf_empty_read(void) { - unsigned char r = csr_readl(0xe0004840); - return r; -} -#define CSR_USB_EP_1_IN_EV_STATUS_ADDR 0xe0004844 -#define CSR_USB_EP_1_IN_EV_STATUS_SIZE 1 -static inline unsigned char usb_ep_1_in_ev_status_read(void) { - unsigned char r = csr_readl(0xe0004844); - return r; -} -static inline void usb_ep_1_in_ev_status_write(unsigned char value) { - csr_writel(value, 0xe0004844); -} -#define CSR_USB_EP_1_IN_EV_PENDING_ADDR 0xe0004848 -#define CSR_USB_EP_1_IN_EV_PENDING_SIZE 1 -static inline unsigned char usb_ep_1_in_ev_pending_read(void) { - unsigned char r = csr_readl(0xe0004848); - return r; -} -static inline void usb_ep_1_in_ev_pending_write(unsigned char value) { - csr_writel(value, 0xe0004848); -} -#define CSR_USB_EP_1_IN_EV_ENABLE_ADDR 0xe000484c -#define CSR_USB_EP_1_IN_EV_ENABLE_SIZE 1 -static inline unsigned char usb_ep_1_in_ev_enable_read(void) { - unsigned char r = csr_readl(0xe000484c); - return r; -} -static inline void usb_ep_1_in_ev_enable_write(unsigned char value) { - csr_writel(value, 0xe000484c); -} -#define CSR_USB_EP_1_IN_LAST_TOK_ADDR 0xe0004850 -#define CSR_USB_EP_1_IN_LAST_TOK_SIZE 1 -static inline unsigned char usb_ep_1_in_last_tok_read(void) { - unsigned char r = csr_readl(0xe0004850); - return r; -} -#define CSR_USB_EP_1_IN_RESPOND_ADDR 0xe0004854 -#define CSR_USB_EP_1_IN_RESPOND_SIZE 1 -static inline unsigned char usb_ep_1_in_respond_read(void) { - unsigned char r = csr_readl(0xe0004854); - return r; -} -static inline void usb_ep_1_in_respond_write(unsigned char value) { - csr_writel(value, 0xe0004854); -} -#define CSR_USB_EP_1_IN_DTB_ADDR 0xe0004858 -#define CSR_USB_EP_1_IN_DTB_SIZE 1 -static inline unsigned char usb_ep_1_in_dtb_read(void) { - unsigned char r = csr_readl(0xe0004858); - return r; -} -static inline void usb_ep_1_in_dtb_write(unsigned char value) { - csr_writel(value, 0xe0004858); -} -#define CSR_USB_EP_1_IN_IBUF_HEAD_ADDR 0xe000485c -#define CSR_USB_EP_1_IN_IBUF_HEAD_SIZE 1 -static inline unsigned char usb_ep_1_in_ibuf_head_read(void) { - unsigned char r = csr_readl(0xe000485c); - return r; -} -static inline void usb_ep_1_in_ibuf_head_write(unsigned char value) { - csr_writel(value, 0xe000485c); -} -#define CSR_USB_EP_1_IN_IBUF_EMPTY_ADDR 0xe0004860 -#define CSR_USB_EP_1_IN_IBUF_EMPTY_SIZE 1 -static inline unsigned char usb_ep_1_in_ibuf_empty_read(void) { - unsigned char r = csr_readl(0xe0004860); - return r; -} -#define CSR_USB_EP_2_OUT_EV_STATUS_ADDR 0xe0004864 -#define CSR_USB_EP_2_OUT_EV_STATUS_SIZE 1 -static inline unsigned char usb_ep_2_out_ev_status_read(void) { - unsigned char r = csr_readl(0xe0004864); - return r; -} -static inline void usb_ep_2_out_ev_status_write(unsigned char value) { - csr_writel(value, 0xe0004864); -} -#define CSR_USB_EP_2_OUT_EV_PENDING_ADDR 0xe0004868 -#define CSR_USB_EP_2_OUT_EV_PENDING_SIZE 1 -static inline unsigned char usb_ep_2_out_ev_pending_read(void) { - unsigned char r = csr_readl(0xe0004868); - return r; -} -static inline void usb_ep_2_out_ev_pending_write(unsigned char value) { - csr_writel(value, 0xe0004868); -} -#define CSR_USB_EP_2_OUT_EV_ENABLE_ADDR 0xe000486c -#define CSR_USB_EP_2_OUT_EV_ENABLE_SIZE 1 -static inline unsigned char usb_ep_2_out_ev_enable_read(void) { - unsigned char r = csr_readl(0xe000486c); - return r; -} -static inline void usb_ep_2_out_ev_enable_write(unsigned char value) { - csr_writel(value, 0xe000486c); -} -#define CSR_USB_EP_2_OUT_LAST_TOK_ADDR 0xe0004870 -#define CSR_USB_EP_2_OUT_LAST_TOK_SIZE 1 -static inline unsigned char usb_ep_2_out_last_tok_read(void) { - unsigned char r = csr_readl(0xe0004870); - return r; -} -#define CSR_USB_EP_2_OUT_RESPOND_ADDR 0xe0004874 -#define CSR_USB_EP_2_OUT_RESPOND_SIZE 1 -static inline unsigned char usb_ep_2_out_respond_read(void) { - unsigned char r = csr_readl(0xe0004874); - return r; -} -static inline void usb_ep_2_out_respond_write(unsigned char value) { - csr_writel(value, 0xe0004874); -} -#define CSR_USB_EP_2_OUT_DTB_ADDR 0xe0004878 -#define CSR_USB_EP_2_OUT_DTB_SIZE 1 -static inline unsigned char usb_ep_2_out_dtb_read(void) { - unsigned char r = csr_readl(0xe0004878); - return r; -} -static inline void usb_ep_2_out_dtb_write(unsigned char value) { - csr_writel(value, 0xe0004878); -} -#define CSR_USB_EP_2_OUT_OBUF_HEAD_ADDR 0xe000487c -#define CSR_USB_EP_2_OUT_OBUF_HEAD_SIZE 1 -static inline unsigned char usb_ep_2_out_obuf_head_read(void) { - unsigned char r = csr_readl(0xe000487c); - return r; -} -static inline void usb_ep_2_out_obuf_head_write(unsigned char value) { - csr_writel(value, 0xe000487c); -} -#define CSR_USB_EP_2_OUT_OBUF_EMPTY_ADDR 0xe0004880 -#define CSR_USB_EP_2_OUT_OBUF_EMPTY_SIZE 1 -static inline unsigned char usb_ep_2_out_obuf_empty_read(void) { - unsigned char r = csr_readl(0xe0004880); - return r; -} -#define CSR_USB_EP_2_IN_EV_STATUS_ADDR 0xe0004884 -#define CSR_USB_EP_2_IN_EV_STATUS_SIZE 1 -static inline unsigned char usb_ep_2_in_ev_status_read(void) { - unsigned char r = csr_readl(0xe0004884); - return r; -} -static inline void usb_ep_2_in_ev_status_write(unsigned char value) { - csr_writel(value, 0xe0004884); -} -#define CSR_USB_EP_2_IN_EV_PENDING_ADDR 0xe0004888 -#define CSR_USB_EP_2_IN_EV_PENDING_SIZE 1 -static inline unsigned char usb_ep_2_in_ev_pending_read(void) { - unsigned char r = csr_readl(0xe0004888); - return r; -} -static inline void usb_ep_2_in_ev_pending_write(unsigned char value) { - csr_writel(value, 0xe0004888); -} -#define CSR_USB_EP_2_IN_EV_ENABLE_ADDR 0xe000488c -#define CSR_USB_EP_2_IN_EV_ENABLE_SIZE 1 -static inline unsigned char usb_ep_2_in_ev_enable_read(void) { - unsigned char r = csr_readl(0xe000488c); - return r; -} -static inline void usb_ep_2_in_ev_enable_write(unsigned char value) { - csr_writel(value, 0xe000488c); -} -#define CSR_USB_EP_2_IN_LAST_TOK_ADDR 0xe0004890 -#define CSR_USB_EP_2_IN_LAST_TOK_SIZE 1 -static inline unsigned char usb_ep_2_in_last_tok_read(void) { - unsigned char r = csr_readl(0xe0004890); - return r; -} -#define CSR_USB_EP_2_IN_RESPOND_ADDR 0xe0004894 -#define CSR_USB_EP_2_IN_RESPOND_SIZE 1 -static inline unsigned char usb_ep_2_in_respond_read(void) { - unsigned char r = csr_readl(0xe0004894); - return r; -} -static inline void usb_ep_2_in_respond_write(unsigned char value) { - csr_writel(value, 0xe0004894); -} -#define CSR_USB_EP_2_IN_DTB_ADDR 0xe0004898 -#define CSR_USB_EP_2_IN_DTB_SIZE 1 -static inline unsigned char usb_ep_2_in_dtb_read(void) { - unsigned char r = csr_readl(0xe0004898); - return r; -} -static inline void usb_ep_2_in_dtb_write(unsigned char value) { - csr_writel(value, 0xe0004898); -} -#define CSR_USB_EP_2_IN_IBUF_HEAD_ADDR 0xe000489c -#define CSR_USB_EP_2_IN_IBUF_HEAD_SIZE 1 -static inline unsigned char usb_ep_2_in_ibuf_head_read(void) { - unsigned char r = csr_readl(0xe000489c); - return r; -} -static inline void usb_ep_2_in_ibuf_head_write(unsigned char value) { - csr_writel(value, 0xe000489c); -} -#define CSR_USB_EP_2_IN_IBUF_EMPTY_ADDR 0xe00048a0 -#define CSR_USB_EP_2_IN_IBUF_EMPTY_SIZE 1 -static inline unsigned char usb_ep_2_in_ibuf_empty_read(void) { - unsigned char r = csr_readl(0xe00048a0); - return r; -} - -/* constants */ -#define NMI_INTERRUPT 0 -static inline int nmi_interrupt_read(void) { - return 0; -} -#define TIMER0_INTERRUPT 1 -static inline int timer0_interrupt_read(void) { - return 1; -} -#define UART_INTERRUPT 2 -static inline int uart_interrupt_read(void) { - return 2; -} -#define USB_INTERRUPT 3 -static inline int usb_interrupt_read(void) { - return 3; -} -#define CSR_DATA_WIDTH 8 -static inline int csr_data_width_read(void) { - return 8; -} -#define SYSTEM_CLOCK_FREQUENCY 12000000 -static inline int system_clock_frequency_read(void) { - return 12000000; -} -#define ROM_DISABLE 1 -static inline int rom_disable_read(void) { - return 1; -} -#define CONFIG_CLOCK_FREQUENCY 12000000 -static inline int config_clock_frequency_read(void) { - return 12000000; -} -#define CONFIG_CPU_RESET_ADDR 0 -static inline int config_cpu_reset_addr_read(void) { - return 0; -} -#define CONFIG_CPU_TYPE "VEXRISCV" -static inline const char * config_cpu_type_read(void) { - return "VEXRISCV"; -} -#define CONFIG_CPU_VARIANT "VEXRISCV" -static inline const char * config_cpu_variant_read(void) { - return "VEXRISCV"; -} -#define CONFIG_CSR_DATA_WIDTH 8 -static inline int config_csr_data_width_read(void) { - return 8; -} - -#endif +#ifndef __GENERATED_CSR_H +#define __GENERATED_CSR_H +#include +#ifdef CSR_ACCESSORS_DEFINED +extern void csr_writeb(uint8_t value, uint32_t addr); +extern uint8_t csr_readb(uint32_t addr); +extern void csr_writew(uint16_t value, uint32_t addr); +extern uint16_t csr_readw(uint32_t addr); +extern void csr_writel(uint32_t value, uint32_t addr); +extern uint32_t csr_readl(uint32_t addr); +#else /* ! CSR_ACCESSORS_DEFINED */ +#include +#endif /* ! CSR_ACCESSORS_DEFINED */ + +/* ctrl */ +#define CSR_CTRL_BASE 0xe0000000 +#define CSR_CTRL_RESET_ADDR 0xe0000000 +#define CSR_CTRL_RESET_SIZE 1 +static inline unsigned char ctrl_reset_read(void) { + unsigned char r = csr_readl(0xe0000000); + return r; +} +static inline void ctrl_reset_write(unsigned char value) { + csr_writel(value, 0xe0000000); +} +#define CSR_CTRL_SCRATCH_ADDR 0xe0000004 +#define CSR_CTRL_SCRATCH_SIZE 4 +static inline unsigned int ctrl_scratch_read(void) { + unsigned int r = csr_readl(0xe0000004); + r <<= 8; + r |= csr_readl(0xe0000008); + r <<= 8; + r |= csr_readl(0xe000000c); + r <<= 8; + r |= csr_readl(0xe0000010); + return r; +} +static inline void ctrl_scratch_write(unsigned int value) { + csr_writel(value >> 24, 0xe0000004); + csr_writel(value >> 16, 0xe0000008); + csr_writel(value >> 8, 0xe000000c); + csr_writel(value, 0xe0000010); +} +#define CSR_CTRL_BUS_ERRORS_ADDR 0xe0000014 +#define CSR_CTRL_BUS_ERRORS_SIZE 4 +static inline unsigned int ctrl_bus_errors_read(void) { + unsigned int r = csr_readl(0xe0000014); + r <<= 8; + r |= csr_readl(0xe0000018); + r <<= 8; + r |= csr_readl(0xe000001c); + r <<= 8; + r |= csr_readl(0xe0000020); + return r; +} + +/* picorvspi */ +#define CSR_PICORVSPI_BASE 0xe0005000 +#define CSR_PICORVSPI_CFG1_ADDR 0xe0005000 +#define CSR_PICORVSPI_CFG1_SIZE 1 +static inline unsigned char picorvspi_cfg1_read(void) { + unsigned char r = csr_readl(0xe0005000); + return r; +} +static inline void picorvspi_cfg1_write(unsigned char value) { + csr_writel(value, 0xe0005000); +} +#define CSR_PICORVSPI_CFG2_ADDR 0xe0005004 +#define CSR_PICORVSPI_CFG2_SIZE 1 +static inline unsigned char picorvspi_cfg2_read(void) { + unsigned char r = csr_readl(0xe0005004); + return r; +} +static inline void picorvspi_cfg2_write(unsigned char value) { + csr_writel(value, 0xe0005004); +} +#define CSR_PICORVSPI_CFG3_ADDR 0xe0005008 +#define CSR_PICORVSPI_CFG3_SIZE 1 +static inline unsigned char picorvspi_cfg3_read(void) { + unsigned char r = csr_readl(0xe0005008); + return r; +} +static inline void picorvspi_cfg3_write(unsigned char value) { + csr_writel(value, 0xe0005008); +} +#define CSR_PICORVSPI_CFG4_ADDR 0xe000500c +#define CSR_PICORVSPI_CFG4_SIZE 1 +static inline unsigned char picorvspi_cfg4_read(void) { + unsigned char r = csr_readl(0xe000500c); + return r; +} +static inline void picorvspi_cfg4_write(unsigned char value) { + csr_writel(value, 0xe000500c); +} +#define CSR_PICORVSPI_STAT1_ADDR 0xe0005010 +#define CSR_PICORVSPI_STAT1_SIZE 1 +static inline unsigned char picorvspi_stat1_read(void) { + unsigned char r = csr_readl(0xe0005010); + return r; +} +#define CSR_PICORVSPI_STAT2_ADDR 0xe0005014 +#define CSR_PICORVSPI_STAT2_SIZE 1 +static inline unsigned char picorvspi_stat2_read(void) { + unsigned char r = csr_readl(0xe0005014); + return r; +} +#define CSR_PICORVSPI_STAT3_ADDR 0xe0005018 +#define CSR_PICORVSPI_STAT3_SIZE 1 +static inline unsigned char picorvspi_stat3_read(void) { + unsigned char r = csr_readl(0xe0005018); + return r; +} +#define CSR_PICORVSPI_STAT4_ADDR 0xe000501c +#define CSR_PICORVSPI_STAT4_SIZE 1 +static inline unsigned char picorvspi_stat4_read(void) { + unsigned char r = csr_readl(0xe000501c); + return r; +} + +/* reboot */ +#define CSR_REBOOT_BASE 0xe0005800 +#define CSR_REBOOT_CTRL_ADDR 0xe0005800 +#define CSR_REBOOT_CTRL_SIZE 1 +static inline unsigned char reboot_ctrl_read(void) { + unsigned char r = csr_readl(0xe0005800); + return r; +} +static inline void reboot_ctrl_write(unsigned char value) { + csr_writel(value, 0xe0005800); +} + +/* rgb */ +#define CSR_RGB_BASE 0xe0006000 +#define CSR_RGB_DAT_ADDR 0xe0006000 +#define CSR_RGB_DAT_SIZE 1 +static inline unsigned char rgb_dat_read(void) { + unsigned char r = csr_readl(0xe0006000); + return r; +} +static inline void rgb_dat_write(unsigned char value) { + csr_writel(value, 0xe0006000); +} +#define CSR_RGB_ADDR_ADDR 0xe0006004 +#define CSR_RGB_ADDR_SIZE 1 +static inline unsigned char rgb_addr_read(void) { + unsigned char r = csr_readl(0xe0006004); + return r; +} +static inline void rgb_addr_write(unsigned char value) { + csr_writel(value, 0xe0006004); +} +#define CSR_RGB_CTRL_ADDR 0xe0006008 +#define CSR_RGB_CTRL_SIZE 1 +static inline unsigned char rgb_ctrl_read(void) { + unsigned char r = csr_readl(0xe0006008); + return r; +} +static inline void rgb_ctrl_write(unsigned char value) { + csr_writel(value, 0xe0006008); +} + +/* timer0 */ +#define CSR_TIMER0_BASE 0xe0002800 +#define CSR_TIMER0_LOAD_ADDR 0xe0002800 +#define CSR_TIMER0_LOAD_SIZE 4 +static inline unsigned int timer0_load_read(void) { + unsigned int r = csr_readl(0xe0002800); + r <<= 8; + r |= csr_readl(0xe0002804); + r <<= 8; + r |= csr_readl(0xe0002808); + r <<= 8; + r |= csr_readl(0xe000280c); + return r; +} +static inline void timer0_load_write(unsigned int value) { + csr_writel(value >> 24, 0xe0002800); + csr_writel(value >> 16, 0xe0002804); + csr_writel(value >> 8, 0xe0002808); + csr_writel(value, 0xe000280c); +} +#define CSR_TIMER0_RELOAD_ADDR 0xe0002810 +#define CSR_TIMER0_RELOAD_SIZE 4 +static inline unsigned int timer0_reload_read(void) { + unsigned int r = csr_readl(0xe0002810); + r <<= 8; + r |= csr_readl(0xe0002814); + r <<= 8; + r |= csr_readl(0xe0002818); + r <<= 8; + r |= csr_readl(0xe000281c); + return r; +} +static inline void timer0_reload_write(unsigned int value) { + csr_writel(value >> 24, 0xe0002810); + csr_writel(value >> 16, 0xe0002814); + csr_writel(value >> 8, 0xe0002818); + csr_writel(value, 0xe000281c); +} +#define CSR_TIMER0_EN_ADDR 0xe0002820 +#define CSR_TIMER0_EN_SIZE 1 +static inline unsigned char timer0_en_read(void) { + unsigned char r = csr_readl(0xe0002820); + return r; +} +static inline void timer0_en_write(unsigned char value) { + csr_writel(value, 0xe0002820); +} +#define CSR_TIMER0_UPDATE_VALUE_ADDR 0xe0002824 +#define CSR_TIMER0_UPDATE_VALUE_SIZE 1 +static inline unsigned char timer0_update_value_read(void) { + unsigned char r = csr_readl(0xe0002824); + return r; +} +static inline void timer0_update_value_write(unsigned char value) { + csr_writel(value, 0xe0002824); +} +#define CSR_TIMER0_VALUE_ADDR 0xe0002828 +#define CSR_TIMER0_VALUE_SIZE 4 +static inline unsigned int timer0_value_read(void) { + unsigned int r = csr_readl(0xe0002828); + r <<= 8; + r |= csr_readl(0xe000282c); + r <<= 8; + r |= csr_readl(0xe0002830); + r <<= 8; + r |= csr_readl(0xe0002834); + return r; +} +#define CSR_TIMER0_EV_STATUS_ADDR 0xe0002838 +#define CSR_TIMER0_EV_STATUS_SIZE 1 +static inline unsigned char timer0_ev_status_read(void) { + unsigned char r = csr_readl(0xe0002838); + return r; +} +static inline void timer0_ev_status_write(unsigned char value) { + csr_writel(value, 0xe0002838); +} +#define CSR_TIMER0_EV_PENDING_ADDR 0xe000283c +#define CSR_TIMER0_EV_PENDING_SIZE 1 +static inline unsigned char timer0_ev_pending_read(void) { + unsigned char r = csr_readl(0xe000283c); + return r; +} +static inline void timer0_ev_pending_write(unsigned char value) { + csr_writel(value, 0xe000283c); +} +#define CSR_TIMER0_EV_ENABLE_ADDR 0xe0002840 +#define CSR_TIMER0_EV_ENABLE_SIZE 1 +static inline unsigned char timer0_ev_enable_read(void) { + unsigned char r = csr_readl(0xe0002840); + return r; +} +static inline void timer0_ev_enable_write(unsigned char value) { + csr_writel(value, 0xe0002840); +} + +/* usb */ +#define CSR_USB_BASE 0xe0004800 +#define CSR_USB_PULLUP_OUT_ADDR 0xe0004800 +#define CSR_USB_PULLUP_OUT_SIZE 1 +static inline unsigned char usb_pullup_out_read(void) { + unsigned char r = csr_readl(0xe0004800); + return r; +} +static inline void usb_pullup_out_write(unsigned char value) { + csr_writel(value, 0xe0004800); +} +#define CSR_USB_EP_0_OUT_EV_STATUS_ADDR 0xe0004804 +#define CSR_USB_EP_0_OUT_EV_STATUS_SIZE 1 +static inline unsigned char usb_ep_0_out_ev_status_read(void) { + unsigned char r = csr_readl(0xe0004804); + return r; +} +static inline void usb_ep_0_out_ev_status_write(unsigned char value) { + csr_writel(value, 0xe0004804); +} +#define CSR_USB_EP_0_OUT_EV_PENDING_ADDR 0xe0004808 +#define CSR_USB_EP_0_OUT_EV_PENDING_SIZE 1 +static inline unsigned char usb_ep_0_out_ev_pending_read(void) { + unsigned char r = csr_readl(0xe0004808); + return r; +} +static inline void usb_ep_0_out_ev_pending_write(unsigned char value) { + csr_writel(value, 0xe0004808); +} +#define CSR_USB_EP_0_OUT_EV_ENABLE_ADDR 0xe000480c +#define CSR_USB_EP_0_OUT_EV_ENABLE_SIZE 1 +static inline unsigned char usb_ep_0_out_ev_enable_read(void) { + unsigned char r = csr_readl(0xe000480c); + return r; +} +static inline void usb_ep_0_out_ev_enable_write(unsigned char value) { + csr_writel(value, 0xe000480c); +} +#define CSR_USB_EP_0_OUT_LAST_TOK_ADDR 0xe0004810 +#define CSR_USB_EP_0_OUT_LAST_TOK_SIZE 1 +static inline unsigned char usb_ep_0_out_last_tok_read(void) { + unsigned char r = csr_readl(0xe0004810); + return r; +} +#define CSR_USB_EP_0_OUT_RESPOND_ADDR 0xe0004814 +#define CSR_USB_EP_0_OUT_RESPOND_SIZE 1 +static inline unsigned char usb_ep_0_out_respond_read(void) { + unsigned char r = csr_readl(0xe0004814); + return r; +} +static inline void usb_ep_0_out_respond_write(unsigned char value) { + csr_writel(value, 0xe0004814); +} +#define CSR_USB_EP_0_OUT_DTB_ADDR 0xe0004818 +#define CSR_USB_EP_0_OUT_DTB_SIZE 1 +static inline unsigned char usb_ep_0_out_dtb_read(void) { + unsigned char r = csr_readl(0xe0004818); + return r; +} +static inline void usb_ep_0_out_dtb_write(unsigned char value) { + csr_writel(value, 0xe0004818); +} +#define CSR_USB_EP_0_OUT_OBUF_HEAD_ADDR 0xe000481c +#define CSR_USB_EP_0_OUT_OBUF_HEAD_SIZE 1 +static inline unsigned char usb_ep_0_out_obuf_head_read(void) { + unsigned char r = csr_readl(0xe000481c); + return r; +} +static inline void usb_ep_0_out_obuf_head_write(unsigned char value) { + csr_writel(value, 0xe000481c); +} +#define CSR_USB_EP_0_OUT_OBUF_EMPTY_ADDR 0xe0004820 +#define CSR_USB_EP_0_OUT_OBUF_EMPTY_SIZE 1 +static inline unsigned char usb_ep_0_out_obuf_empty_read(void) { + unsigned char r = csr_readl(0xe0004820); + return r; +} +#define CSR_USB_EP_0_IN_EV_STATUS_ADDR 0xe0004824 +#define CSR_USB_EP_0_IN_EV_STATUS_SIZE 1 +static inline unsigned char usb_ep_0_in_ev_status_read(void) { + unsigned char r = csr_readl(0xe0004824); + return r; +} +static inline void usb_ep_0_in_ev_status_write(unsigned char value) { + csr_writel(value, 0xe0004824); +} +#define CSR_USB_EP_0_IN_EV_PENDING_ADDR 0xe0004828 +#define CSR_USB_EP_0_IN_EV_PENDING_SIZE 1 +static inline unsigned char usb_ep_0_in_ev_pending_read(void) { + unsigned char r = csr_readl(0xe0004828); + return r; +} +static inline void usb_ep_0_in_ev_pending_write(unsigned char value) { + csr_writel(value, 0xe0004828); +} +#define CSR_USB_EP_0_IN_EV_ENABLE_ADDR 0xe000482c +#define CSR_USB_EP_0_IN_EV_ENABLE_SIZE 1 +static inline unsigned char usb_ep_0_in_ev_enable_read(void) { + unsigned char r = csr_readl(0xe000482c); + return r; +} +static inline void usb_ep_0_in_ev_enable_write(unsigned char value) { + csr_writel(value, 0xe000482c); +} +#define CSR_USB_EP_0_IN_LAST_TOK_ADDR 0xe0004830 +#define CSR_USB_EP_0_IN_LAST_TOK_SIZE 1 +static inline unsigned char usb_ep_0_in_last_tok_read(void) { + unsigned char r = csr_readl(0xe0004830); + return r; +} +#define CSR_USB_EP_0_IN_RESPOND_ADDR 0xe0004834 +#define CSR_USB_EP_0_IN_RESPOND_SIZE 1 +static inline unsigned char usb_ep_0_in_respond_read(void) { + unsigned char r = csr_readl(0xe0004834); + return r; +} +static inline void usb_ep_0_in_respond_write(unsigned char value) { + csr_writel(value, 0xe0004834); +} +#define CSR_USB_EP_0_IN_DTB_ADDR 0xe0004838 +#define CSR_USB_EP_0_IN_DTB_SIZE 1 +static inline unsigned char usb_ep_0_in_dtb_read(void) { + unsigned char r = csr_readl(0xe0004838); + return r; +} +static inline void usb_ep_0_in_dtb_write(unsigned char value) { + csr_writel(value, 0xe0004838); +} +#define CSR_USB_EP_0_IN_IBUF_HEAD_ADDR 0xe000483c +#define CSR_USB_EP_0_IN_IBUF_HEAD_SIZE 1 +static inline unsigned char usb_ep_0_in_ibuf_head_read(void) { + unsigned char r = csr_readl(0xe000483c); + return r; +} +static inline void usb_ep_0_in_ibuf_head_write(unsigned char value) { + csr_writel(value, 0xe000483c); +} +#define CSR_USB_EP_0_IN_IBUF_EMPTY_ADDR 0xe0004840 +#define CSR_USB_EP_0_IN_IBUF_EMPTY_SIZE 1 +static inline unsigned char usb_ep_0_in_ibuf_empty_read(void) { + unsigned char r = csr_readl(0xe0004840); + return r; +} + +/* constants */ +#define NMI_INTERRUPT 0 +static inline int nmi_interrupt_read(void) { + return 0; +} +#define TIMER0_INTERRUPT 1 +static inline int timer0_interrupt_read(void) { + return 1; +} +#define UART_INTERRUPT 2 +static inline int uart_interrupt_read(void) { + return 2; +} +#define USB_INTERRUPT 3 +static inline int usb_interrupt_read(void) { + return 3; +} +#define CSR_DATA_WIDTH 8 +static inline int csr_data_width_read(void) { + return 8; +} +#define SYSTEM_CLOCK_FREQUENCY 12000000 +static inline int system_clock_frequency_read(void) { + return 12000000; +} +#define ROM_DISABLE 1 +static inline int rom_disable_read(void) { + return 1; +} +#define CONFIG_CLOCK_FREQUENCY 12000000 +static inline int config_clock_frequency_read(void) { + return 12000000; +} +#define CONFIG_CPU_RESET_ADDR 0 +static inline int config_cpu_reset_addr_read(void) { + return 0; +} +#define CONFIG_CPU_TYPE "VEXRISCV" +static inline const char * config_cpu_type_read(void) { + return "VEXRISCV"; +} +#define CONFIG_CPU_VARIANT "VEXRISCV" +static inline const char * config_cpu_variant_read(void) { + return "VEXRISCV"; +} +#define CONFIG_CSR_DATA_WIDTH 8 +static inline int config_csr_data_width_read(void) { + return 8; +} + +#endif diff --git a/sw/include/generated/mem.h b/sw/include/generated/mem.h index c6ec8b4..6fe108e 100644 --- a/sw/include/generated/mem.h +++ b/sw/include/generated/mem.h @@ -1,13 +1,16 @@ -#ifndef __GENERATED_MEM_H -#define __GENERATED_MEM_H - -#define SRAM_BASE 0x10000000 -#define SRAM_SIZE 0x00020000 - -#define ROM_BASE 0x00000000 -#define ROM_SIZE 0x00002000 - -#define SPIFLASH_BASE 0x20000000 -#define SPIFLASH_SIZE 0x00200000 - -#endif +#ifndef __GENERATED_MEM_H +#define __GENERATED_MEM_H + +#define VEXRISCV_DEBUG_BASE 0xf00f0000 +#define VEXRISCV_DEBUG_SIZE 0x00000010 + +#define SRAM_BASE 0x10000000 +#define SRAM_SIZE 0x00020000 + +#define ROM_BASE 0x00000000 +#define ROM_SIZE 0x00002000 + +#define SPIFLASH_BASE 0x20000000 +#define SPIFLASH_SIZE 0x00200000 + +#endif diff --git a/sw/include/system.h b/sw/include/system.h index b3b09bb..ea3da85 100644 --- a/sw/include/system.h +++ b/sw/include/system.h @@ -58,12 +58,9 @@ static inline void mtspr(unsigned long add, unsigned long val) #include -__attribute__((noreturn)) static inline void reboot(void) { - reboot_ctrl_write(0xac); - while (1); -} +__attribute__((noreturn)) void reboot(void); -__attribute__((noreturn)) static inline void reboot_to_image(uint8_t image_index) { +__attribute__((noreturn)) static inline void warmboot_to_image(uint8_t image_index) { reboot_ctrl_write(0xac | (image_index & 3) << 0); while (1); } diff --git a/sw/ld/output_format.ld b/sw/ld/output_format.ld index 269cd64..5e76f5f 100644 --- a/sw/ld/output_format.ld +++ b/sw/ld/output_format.ld @@ -1 +1 @@ -OUTPUT_FORMAT("elf32-littleriscv") +OUTPUT_FORMAT("elf32-littleriscv") diff --git a/sw/ld/regions.ld b/sw/ld/regions.ld index 6fcb9c8..605e8f9 100644 --- a/sw/ld/regions.ld +++ b/sw/ld/regions.ld @@ -1,4 +1,4 @@ -MEMORY { - sram : ORIGIN = 0x10000000, LENGTH = 0x00020000 - rom : ORIGIN = 0x00000000, LENGTH = 0x00002000 -} +MEMORY { + sram : ORIGIN = 0x10000000, LENGTH = 0x00020000 + rom : ORIGIN = 0x00000000, LENGTH = 0x00002000 +} diff --git a/sw/src/main.c b/sw/src/main.c index a4e9910..a6b9433 100644 --- a/sw/src/main.c +++ b/sw/src/main.c @@ -1,113 +1,135 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -struct ff_spi *spi; - -void isr(void) -{ - unsigned int irqs; - - irqs = irq_pending() & irq_getmask(); - - if (irqs & (1 << USB_INTERRUPT)) - usb_isr(); - -#ifdef CSR_UART_BASE - if (irqs & (1 << UART_INTERRUPT)) - uart_isr(); -#endif -} - -#ifdef CSR_UART_BASE -static void rv_putchar(void *ignored, char c) -{ - (void)ignored; - if (c == '\n') - uart_write('\r'); - if (c == '\r') - return; - uart_write(c); -} -#endif - -void reboot_to(uint32_t addr) { - irq_setie(0); - usb_disconnect(); - spiFree(); - rgb_mode_error(); - /* - * Set the return address register to the base of the DDR memory at 0x80000000. - * The reset handler of the application loaded to DDR memory by the bootloader - * is expected to be at that location. - */ - void (*fnc)(void) = (void *)addr; - fnc(); - __builtin_unreachable(); - asm volatile("mv ra,%0\n\t" - : - : "r"(addr) - ); - - /* - * Flush the cache. - */ - // asm volatile ("fence.i"); - - /* - * We need to explicitly execute a return intruction in case the compiler had - * done some return addres register manipulation in this function's veneer. - */ - asm volatile("ret"); - __builtin_unreachable(); -} - -static void init(void) -{ -#ifdef CSR_UART_BASE - init_printf(NULL, rv_putchar); -#endif - irq_setmask(0); - irq_setie(1); - uart_init(); - usb_init(); - dfu_init(); - time_init(); - rgb_init(); - - spi = spiAlloc(); - spiSetPin(spi, SP_MOSI, 0); - spiSetPin(spi, SP_MISO, 1); - spiSetPin(spi, SP_WP, 2); - spiSetPin(spi, SP_HOLD, 3); - spiSetPin(spi, SP_CLK, 4); - spiSetPin(spi, SP_CS, 5); - spiSetPin(spi, SP_D0, 0); - spiSetPin(spi, SP_D1, 1); - spiSetPin(spi, SP_D2, 2); - spiSetPin(spi, SP_D3, 3); - spiInit(spi); -} - -int main(int argc, char **argv) -{ - (void)argc; - (void)argv; - - init(); - - usb_connect(); - while (1) - { - usb_poll(); - dfu_poll(); - } - return 0; +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +struct ff_spi *spi; + +void isr(void) +{ + unsigned int irqs; + + irqs = irq_pending() & irq_getmask(); + + if (irqs & (1 << USB_INTERRUPT)) + usb_isr(); + +#ifdef CSR_UART_BASE + if (irqs & (1 << UART_INTERRUPT)) + uart_isr(); +#endif +} + +#ifdef CSR_UART_BASE +static void rv_putchar(void *ignored, char c) +{ + (void)ignored; + if (c == '\n') + uart_write('\r'); + if (c == '\r') + return; + uart_write(c); +} +#endif + +#define REBOOT_ADDR 0x20040000 +void reboot(void) { + irq_setie(0); + irq_setmask(0); + usb_disconnect(); + spiFree(); + rgb_mode_error(); + + // Check the first few words for the sync pulse; + int i; + int riscv_boot = 1; + uint32_t *destination_array = (uint32_t *)REBOOT_ADDR; + for (i = 0; i < 16; i++) { + if (destination_array[i] == 0x7e99aa7e) { + riscv_boot = 0; + break; + } + } + + if (riscv_boot) { + // Reset the Return Address, zero out some registers, and return. + asm volatile( + "mv ra,%0\n\t" /* x1 */ + "mv sp,zero\n\t" /* x2 */ + "mv gp,zero\n\t" /* x3 */ + "mv tp,zero\n\t" /* x4 */ + "mv t0,zero\n\t" /* x5 */ + "mv t1,zero\n\t" /* x6 */ + "mv t2,zero\n\t" /* x7 */ + "mv x8,zero\n\t" /* x8 */ + "mv s1,zero\n\t" /* x9 */ + "mv a0,zero\n\t" /* x10 */ + "mv a1,zero\n\t" /* x11 */ + + // /* Flush the caches */ + // ".word 0x400f\n\t" + // "nop\n\t" + // "nop\n\t" + // "nop\n\t" + + "ret\n\t" + + : + : "r"(REBOOT_ADDR) + ); + } + else { + // Issue a reboot + warmboot_to_image(2); + } + __builtin_unreachable(); +} + +static void init(void) +{ +#ifdef CSR_UART_BASE + init_printf(NULL, rv_putchar); +#endif + irq_setmask(0); + irq_setie(1); + uart_init(); + usb_init(); + dfu_init(); + time_init(); + rgb_init(); + + spi = spiAlloc(); + spiSetPin(spi, SP_MOSI, 0); + spiSetPin(spi, SP_MISO, 1); + spiSetPin(spi, SP_WP, 2); + spiSetPin(spi, SP_HOLD, 3); + spiSetPin(spi, SP_CLK, 4); + spiSetPin(spi, SP_CS, 5); + spiSetPin(spi, SP_D0, 0); + spiSetPin(spi, SP_D1, 1); + spiSetPin(spi, SP_D2, 2); + spiSetPin(spi, SP_D3, 3); + spiInit(spi); +} + +int main(int argc, char **argv) +{ + (void)argc; + (void)argv; + + init(); + + usb_connect(); + while (1) + { + usb_poll(); + dfu_poll(); + } + return 0; } \ No newline at end of file diff --git a/sw/src/usb-dev.c b/sw/src/usb-dev.c index 585e607..112afe8 100644 --- a/sw/src/usb-dev.c +++ b/sw/src/usb-dev.c @@ -192,10 +192,7 @@ void usb_setup(const struct usb_setup_request *setup) // to be received. usb_ack_in(); usb_wait_for_send_done(); - reboot_to(0x20040000); - - // Issue a reboot - reboot_to_image(0); + reboot(); while (1) ; return; diff --git a/sw/third_party/div.S b/sw/third_party/div.S index ca0d9f4..b837820 100644 --- a/sw/third_party/div.S +++ b/sw/third_party/div.S @@ -1,121 +1,121 @@ - .text - .align 2 - -#ifndef __riscv64 -/* Our RV64 64-bit routines are equivalent to our RV32 32-bit routines. */ -# define __udivdi3 __udivsi3 -# define __umoddi3 __umodsi3 -# define __divdi3 __divsi3 -# define __moddi3 __modsi3 -#else - .globl __udivsi3 -__udivsi3: - /* Compute __udivdi3(a0 << 32, a1 << 32); cast result to uint32_t. */ - sll a0, a0, 32 - sll a1, a1, 32 - move t0, ra - jal __udivdi3 - sext.w a0, a0 - jr t0 - - .globl __umodsi3 -__umodsi3: - /* Compute __udivdi3((uint32_t)a0, (uint32_t)a1); cast a1 to uint32_t. */ - sll a0, a0, 32 - sll a1, a1, 32 - srl a0, a0, 32 - srl a1, a1, 32 - move t0, ra - jal __udivdi3 - sext.w a0, a1 - jr t0 - - .globl __modsi3 - __modsi3 = __moddi3 - - .globl __divsi3 -__divsi3: - /* Check for special case of INT_MIN/-1. Otherwise, fall into __divdi3. */ - li t0, -1 - beq a1, t0, .L20 -#endif - - .globl __divdi3 -__divdi3: - bltz a0, .L10 - bltz a1, .L11 - /* Since the quotient is positive, fall into __udivdi3. */ - - .globl __udivdi3 -__udivdi3: - mv a2, a1 - mv a1, a0 - li a0, -1 - beqz a2, .L5 - li a3, 1 - bgeu a2, a1, .L2 -.L1: - blez a2, .L2 - slli a2, a2, 1 - slli a3, a3, 1 - bgtu a1, a2, .L1 -.L2: - li a0, 0 -.L3: - bltu a1, a2, .L4 - sub a1, a1, a2 - or a0, a0, a3 -.L4: - srli a3, a3, 1 - srli a2, a2, 1 - bnez a3, .L3 -.L5: - ret - - .globl __umoddi3 -__umoddi3: - /* Call __udivdi3(a0, a1), then return the remainder, which is in a1. */ - move t0, ra - jal __udivdi3 - move a0, a1 - jr t0 - - /* Handle negative arguments to __divdi3. */ -.L10: - neg a0, a0 - bgez a1, .L12 /* Compute __udivdi3(-a0, a1), then negate the result. */ - neg a1, a1 - j __divdi3 /* Compute __udivdi3(-a0, -a1). */ -.L11: /* Compute __udivdi3(a0, -a1), then negate the result. */ - neg a1, a1 -.L12: - move t0, ra - jal __divdi3 - neg a0, a0 - jr t0 - - .globl __moddi3 -__moddi3: - move t0, ra - bltz a1, .L31 - bltz a0, .L32 -.L30: - jal __udivdi3 /* The dividend is not negative. */ - move a0, a1 - jr t0 -.L31: - neg a1, a1 - bgez a0, .L30 -.L32: - neg a0, a0 - jal __udivdi3 /* The dividend is hella negative. */ - neg a0, a1 - jr t0 - -#ifdef __riscv64 - /* continuation of __divsi3 */ -.L20: - sll t0, t0, 31 - bne a0, t0, __divdi3 - ret + .text + .align 2 + +#ifndef __riscv64 +/* Our RV64 64-bit routines are equivalent to our RV32 32-bit routines. */ +# define __udivdi3 __udivsi3 +# define __umoddi3 __umodsi3 +# define __divdi3 __divsi3 +# define __moddi3 __modsi3 +#else + .globl __udivsi3 +__udivsi3: + /* Compute __udivdi3(a0 << 32, a1 << 32); cast result to uint32_t. */ + sll a0, a0, 32 + sll a1, a1, 32 + move t0, ra + jal __udivdi3 + sext.w a0, a0 + jr t0 + + .globl __umodsi3 +__umodsi3: + /* Compute __udivdi3((uint32_t)a0, (uint32_t)a1); cast a1 to uint32_t. */ + sll a0, a0, 32 + sll a1, a1, 32 + srl a0, a0, 32 + srl a1, a1, 32 + move t0, ra + jal __udivdi3 + sext.w a0, a1 + jr t0 + + .globl __modsi3 + __modsi3 = __moddi3 + + .globl __divsi3 +__divsi3: + /* Check for special case of INT_MIN/-1. Otherwise, fall into __divdi3. */ + li t0, -1 + beq a1, t0, .L20 +#endif + + .globl __divdi3 +__divdi3: + bltz a0, .L10 + bltz a1, .L11 + /* Since the quotient is positive, fall into __udivdi3. */ + + .globl __udivdi3 +__udivdi3: + mv a2, a1 + mv a1, a0 + li a0, -1 + beqz a2, .L5 + li a3, 1 + bgeu a2, a1, .L2 +.L1: + blez a2, .L2 + slli a2, a2, 1 + slli a3, a3, 1 + bgtu a1, a2, .L1 +.L2: + li a0, 0 +.L3: + bltu a1, a2, .L4 + sub a1, a1, a2 + or a0, a0, a3 +.L4: + srli a3, a3, 1 + srli a2, a2, 1 + bnez a3, .L3 +.L5: + ret + + .globl __umoddi3 +__umoddi3: + /* Call __udivdi3(a0, a1), then return the remainder, which is in a1. */ + move t0, ra + jal __udivdi3 + move a0, a1 + jr t0 + + /* Handle negative arguments to __divdi3. */ +.L10: + neg a0, a0 + bgez a1, .L12 /* Compute __udivdi3(-a0, a1), then negate the result. */ + neg a1, a1 + j __divdi3 /* Compute __udivdi3(-a0, -a1). */ +.L11: /* Compute __udivdi3(a0, -a1), then negate the result. */ + neg a1, a1 +.L12: + move t0, ra + jal __divdi3 + neg a0, a0 + jr t0 + + .globl __moddi3 +__moddi3: + move t0, ra + bltz a1, .L31 + bltz a0, .L32 +.L30: + jal __udivdi3 /* The dividend is not negative. */ + move a0, a1 + jr t0 +.L31: + neg a1, a1 + bgez a0, .L30 +.L32: + neg a0, a0 + jal __udivdi3 /* The dividend is hella negative. */ + neg a0, a1 + jr t0 + +#ifdef __riscv64 + /* continuation of __divsi3 */ +.L20: + sll t0, t0, 31 + bne a0, t0, __divdi3 + ret #endif \ No newline at end of file diff --git a/sw/third_party/libbase/libc.c b/sw/third_party/libbase/libc.c index 4bb3ee0..a2a427b 100644 --- a/sw/third_party/libbase/libc.c +++ b/sw/third_party/libbase/libc.c @@ -1,117 +1,117 @@ -/* $OpenBSD: strlen.c,v 1.8 2014/06/10 04:17:37 deraadt Exp $ */ - -/*- - * Copyright (c) 1990, 1993 - * The Regents of the University of California. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the University nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#include - -size_t -strlen(const char *str) -{ - const char *s; - - for (s = str; *s; ++s) - ; - return (s - str); -} - -/** - * memcpy - Copies one area of memory to another - * @dest: Destination - * @src: Source - * @n: The size to copy. - */ -void *memcpy(void *to, const void *from, size_t n) -{ - void *xto = to; - size_t temp; - - if(!n) - return xto; - if((long)to & 1) { - char *cto = to; - const char *cfrom = from; - *cto++ = *cfrom++; - to = cto; - from = cfrom; - n--; - } - if((long)from & 1) { - char *cto = to; - const char *cfrom = from; - for (; n; n--) - *cto++ = *cfrom++; - return xto; - } - if(n > 2 && (long)to & 2) { - short *sto = to; - const short *sfrom = from; - *sto++ = *sfrom++; - to = sto; - from = sfrom; - n -= 2; - } - if((long)from & 2) { - short *sto = to; - const short *sfrom = from; - temp = n >> 1; - for (; temp; temp--) - *sto++ = *sfrom++; - to = sto; - from = sfrom; - if(n & 1) { - char *cto = to; - const char *cfrom = from; - *cto = *cfrom; - } - return xto; - } - temp = n >> 2; - if(temp) { - long *lto = to; - const long *lfrom = from; - for(; temp; temp--) - *lto++ = *lfrom++; - to = lto; - from = lfrom; - } - if(n & 2) { - short *sto = to; - const short *sfrom = from; - *sto++ = *sfrom++; - to = sto; - from = sfrom; - } - if(n & 1) { - char *cto = to; - const char *cfrom = from; - *cto = *cfrom; - } - return xto; +/* $OpenBSD: strlen.c,v 1.8 2014/06/10 04:17:37 deraadt Exp $ */ + +/*- + * Copyright (c) 1990, 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include + +size_t +strlen(const char *str) +{ + const char *s; + + for (s = str; *s; ++s) + ; + return (s - str); +} + +/** + * memcpy - Copies one area of memory to another + * @dest: Destination + * @src: Source + * @n: The size to copy. + */ +void *memcpy(void *to, const void *from, size_t n) +{ + void *xto = to; + size_t temp; + + if(!n) + return xto; + if((long)to & 1) { + char *cto = to; + const char *cfrom = from; + *cto++ = *cfrom++; + to = cto; + from = cfrom; + n--; + } + if((long)from & 1) { + char *cto = to; + const char *cfrom = from; + for (; n; n--) + *cto++ = *cfrom++; + return xto; + } + if(n > 2 && (long)to & 2) { + short *sto = to; + const short *sfrom = from; + *sto++ = *sfrom++; + to = sto; + from = sfrom; + n -= 2; + } + if((long)from & 2) { + short *sto = to; + const short *sfrom = from; + temp = n >> 1; + for (; temp; temp--) + *sto++ = *sfrom++; + to = sto; + from = sfrom; + if(n & 1) { + char *cto = to; + const char *cfrom = from; + *cto = *cfrom; + } + return xto; + } + temp = n >> 2; + if(temp) { + long *lto = to; + const long *lfrom = from; + for(; temp; temp--) + *lto++ = *lfrom++; + to = lto; + from = lfrom; + } + if(n & 2) { + short *sto = to; + const short *sfrom = from; + *sto++ = *sfrom++; + to = sto; + from = sfrom; + } + if(n & 1) { + char *cto = to; + const char *cfrom = from; + *cto = *cfrom; + } + return xto; } \ No newline at end of file diff --git a/sw/third_party/mul.S b/sw/third_party/mul.S index d44f726..2499abf 100644 --- a/sw/third_party/mul.S +++ b/sw/third_party/mul.S @@ -1,26 +1,26 @@ - .text - .align 2 - -#ifdef __riscv64 -#define _RISCV_SZPTR 64 -#define _RISCV_SZINT 64 -#else -/* Our RV64 64-bit routine is equivalent to our RV32 32-bit routine. */ -# define __muldi3 __mulsi3 -#define _RISCV_SZPTR 32 -#define _RISCV_SZINT 32 -#endif - - .globl __muldi3 -__muldi3: - mv a2, a0 - li a0, 0 -.L1: - slli a3, a1, _RISCV_SZPTR-1 - bgez a3, .L2 - add a0, a0, a2 -.L2: - srli a1, a1, 1 - slli a2, a2, 1 - bnez a1, .L1 - ret + .text + .align 2 + +#ifdef __riscv64 +#define _RISCV_SZPTR 64 +#define _RISCV_SZINT 64 +#else +/* Our RV64 64-bit routine is equivalent to our RV32 32-bit routine. */ +# define __muldi3 __mulsi3 +#define _RISCV_SZPTR 32 +#define _RISCV_SZINT 32 +#endif + + .globl __muldi3 +__muldi3: + mv a2, a0 + li a0, 0 +.L1: + slli a3, a1, _RISCV_SZPTR-1 + bgez a3, .L2 + add a0, a0, a2 +.L2: + srli a1, a1, 1 + slli a2, a2, 1 + bnez a1, .L1 + ret diff --git a/sw/third_party/printf.c b/sw/third_party/printf.c index 4af20f4..f505ef8 100644 --- a/sw/third_party/printf.c +++ b/sw/third_party/printf.c @@ -1,268 +1,268 @@ -/* - * Copyright (c) 2004,2012 Kustaa Nyholm / SpareTimeLabs - * - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without modification, - * are permitted provided that the following conditions are met: - * - * Redistributions of source code must retain the above copyright notice, this list - * of conditions and the following disclaimer. - * - * Redistributions in binary form must reproduce the above copyright notice, this - * list of conditions and the following disclaimer in the documentation and/or other - * materials provided with the distribution. - * - * Neither the name of the Kustaa Nyholm or SpareTimeLabs nor the names of its - * contributors may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, - * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, - * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY - * OF SUCH DAMAGE. - */ - -#include "printf.h" - -typedef void (*putcf)(void *, char); -static putcf stdout_putf; -static void *stdout_putp; - -#ifdef PRINTF_LONG_SUPPORT - -static void uli2a(unsigned long int num, unsigned int base, int uc, char *bf) -{ - int n = 0; - unsigned int d = 1; - while (num / d >= base) - d *= base; - while (d != 0) - { - int dgt = num / d; - num %= d; - d /= base; - if (n || dgt > 0 || d == 0) - { - *bf++ = dgt + (dgt < 10 ? '0' : (uc ? 'A' : 'a') - 10); - ++n; - } - } - *bf = 0; -} - -static void li2a(long num, char *bf) -{ - if (num < 0) - { - num = -num; - *bf++ = '-'; - } - uli2a(num, 10, 0, bf); -} - -#endif - -static void ui2a(unsigned int num, unsigned int base, int uc, char *bf) -{ - int n = 0; - unsigned int d = 1; - while (num / d >= base) - d *= base; - while (d != 0) - { - int dgt = num / d; - num %= d; - d /= base; - if (n || dgt > 0 || d == 0) - { - *bf++ = dgt + (dgt < 10 ? '0' : (uc ? 'A' : 'a') - 10); - ++n; - } - } - *bf = 0; -} - -static void i2a(int num, char *bf) -{ - if (num < 0) - { - num = -num; - *bf++ = '-'; - } - ui2a(num, 10, 0, bf); -} - -static int a2d(char ch) -{ - if (ch >= '0' && ch <= '9') - return ch - '0'; - else if (ch >= 'a' && ch <= 'f') - return ch - 'a' + 10; - else if (ch >= 'A' && ch <= 'F') - return ch - 'A' + 10; - else - return -1; -} - -static char a2i(char ch, char **src, int base, int *nump) -{ - char *p = *src; - int num = 0; - int digit; - while ((digit = a2d(ch)) >= 0) - { - if (digit > base) - break; - num = num * base + digit; - ch = *p++; - } - *src = p; - *nump = num; - return ch; -} - -static void putchw(void *putp, putcf putf, int n, char z, char *bf) -{ - char fc = z ? '0' : ' '; - char ch; - char *p = bf; - while (*p++ && n > 0) - n--; - while (n-- > 0) - putf(putp, fc); - while ((ch = *bf++)) - putf(putp, ch); -} - -void tfp_format(void *putp, putcf putf, char *fmt, va_list va) -{ - char bf[12]; - - char ch; - - while ((ch = *(fmt++))) - { - if (ch != '%') - putf(putp, ch); - else - { - char lz = 0; -#ifdef PRINTF_LONG_SUPPORT - char lng = 0; -#endif - int w = 0; - ch = *(fmt++); - if (ch == '0') - { - ch = *(fmt++); - lz = 1; - } - if (ch >= '0' && ch <= '9') - { - ch = a2i(ch, &fmt, 10, &w); - } -#ifdef PRINTF_LONG_SUPPORT - if (ch == 'l') - { - ch = *(fmt++); - lng = 1; - } -#endif - switch (ch) - { - case 0: - goto abort; - case 'u': - { -#ifdef PRINTF_LONG_SUPPORT - if (lng) - uli2a(va_arg(va, unsigned long int), 10, 0, bf); - else -#endif - ui2a(va_arg(va, unsigned int), 10, 0, bf); - putchw(putp, putf, w, lz, bf); - break; - } - case 'd': - { -#ifdef PRINTF_LONG_SUPPORT - if (lng) - li2a(va_arg(va, unsigned long int), bf); - else -#endif - i2a(va_arg(va, int), bf); - putchw(putp, putf, w, lz, bf); - break; - } - case 'x': - case 'X': -#ifdef PRINTF_LONG_SUPPORT - if (lng) - uli2a(va_arg(va, unsigned long int), 16, (ch == 'X'), bf); - else -#endif - ui2a(va_arg(va, unsigned int), 16, (ch == 'X'), bf); - putchw(putp, putf, w, lz, bf); - break; - case 'c': - putf(putp, (char)(va_arg(va, int))); - break; - case 's': - putchw(putp, putf, w, 0, va_arg(va, char *)); - break; - case '%': - putf(putp, ch); - default: - break; - } - } - } -abort:; -} - -void init_printf(void *putp, void (*putf)(void *, char)) -{ - stdout_putf = putf; - stdout_putp = putp; -} - -void tfp_printf(char *fmt, ...) -{ - va_list va; - va_start(va, fmt); - tfp_format(stdout_putp, stdout_putf, fmt, va); - va_end(va); -} - -static void putcp(void *p, char c) -{ - *(*((char **)p))++ = c; -} - -void tfp_sprintf(char *s, char *fmt, ...) -{ - va_list va; - va_start(va, fmt); - tfp_format(&s, putcp, fmt, va); - putcp(&s, 0); - va_end(va); -} - -int puts(const char *s) { - puts_noendl(s); - stdout_putf(stdout_putp, '\n'); - return 1; -} - -int puts_noendl(const char *s) { - while (*s) - stdout_putf(stdout_putp, *s++); - return 1; +/* + * Copyright (c) 2004,2012 Kustaa Nyholm / SpareTimeLabs + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, this list + * of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, this + * list of conditions and the following disclaimer in the documentation and/or other + * materials provided with the distribution. + * + * Neither the name of the Kustaa Nyholm or SpareTimeLabs nor the names of its + * contributors may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, + * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY + * OF SUCH DAMAGE. + */ + +#include "printf.h" + +typedef void (*putcf)(void *, char); +static putcf stdout_putf; +static void *stdout_putp; + +#ifdef PRINTF_LONG_SUPPORT + +static void uli2a(unsigned long int num, unsigned int base, int uc, char *bf) +{ + int n = 0; + unsigned int d = 1; + while (num / d >= base) + d *= base; + while (d != 0) + { + int dgt = num / d; + num %= d; + d /= base; + if (n || dgt > 0 || d == 0) + { + *bf++ = dgt + (dgt < 10 ? '0' : (uc ? 'A' : 'a') - 10); + ++n; + } + } + *bf = 0; +} + +static void li2a(long num, char *bf) +{ + if (num < 0) + { + num = -num; + *bf++ = '-'; + } + uli2a(num, 10, 0, bf); +} + +#endif + +static void ui2a(unsigned int num, unsigned int base, int uc, char *bf) +{ + int n = 0; + unsigned int d = 1; + while (num / d >= base) + d *= base; + while (d != 0) + { + int dgt = num / d; + num %= d; + d /= base; + if (n || dgt > 0 || d == 0) + { + *bf++ = dgt + (dgt < 10 ? '0' : (uc ? 'A' : 'a') - 10); + ++n; + } + } + *bf = 0; +} + +static void i2a(int num, char *bf) +{ + if (num < 0) + { + num = -num; + *bf++ = '-'; + } + ui2a(num, 10, 0, bf); +} + +static int a2d(char ch) +{ + if (ch >= '0' && ch <= '9') + return ch - '0'; + else if (ch >= 'a' && ch <= 'f') + return ch - 'a' + 10; + else if (ch >= 'A' && ch <= 'F') + return ch - 'A' + 10; + else + return -1; +} + +static char a2i(char ch, char **src, int base, int *nump) +{ + char *p = *src; + int num = 0; + int digit; + while ((digit = a2d(ch)) >= 0) + { + if (digit > base) + break; + num = num * base + digit; + ch = *p++; + } + *src = p; + *nump = num; + return ch; +} + +static void putchw(void *putp, putcf putf, int n, char z, char *bf) +{ + char fc = z ? '0' : ' '; + char ch; + char *p = bf; + while (*p++ && n > 0) + n--; + while (n-- > 0) + putf(putp, fc); + while ((ch = *bf++)) + putf(putp, ch); +} + +void tfp_format(void *putp, putcf putf, char *fmt, va_list va) +{ + char bf[12]; + + char ch; + + while ((ch = *(fmt++))) + { + if (ch != '%') + putf(putp, ch); + else + { + char lz = 0; +#ifdef PRINTF_LONG_SUPPORT + char lng = 0; +#endif + int w = 0; + ch = *(fmt++); + if (ch == '0') + { + ch = *(fmt++); + lz = 1; + } + if (ch >= '0' && ch <= '9') + { + ch = a2i(ch, &fmt, 10, &w); + } +#ifdef PRINTF_LONG_SUPPORT + if (ch == 'l') + { + ch = *(fmt++); + lng = 1; + } +#endif + switch (ch) + { + case 0: + goto abort; + case 'u': + { +#ifdef PRINTF_LONG_SUPPORT + if (lng) + uli2a(va_arg(va, unsigned long int), 10, 0, bf); + else +#endif + ui2a(va_arg(va, unsigned int), 10, 0, bf); + putchw(putp, putf, w, lz, bf); + break; + } + case 'd': + { +#ifdef PRINTF_LONG_SUPPORT + if (lng) + li2a(va_arg(va, unsigned long int), bf); + else +#endif + i2a(va_arg(va, int), bf); + putchw(putp, putf, w, lz, bf); + break; + } + case 'x': + case 'X': +#ifdef PRINTF_LONG_SUPPORT + if (lng) + uli2a(va_arg(va, unsigned long int), 16, (ch == 'X'), bf); + else +#endif + ui2a(va_arg(va, unsigned int), 16, (ch == 'X'), bf); + putchw(putp, putf, w, lz, bf); + break; + case 'c': + putf(putp, (char)(va_arg(va, int))); + break; + case 's': + putchw(putp, putf, w, 0, va_arg(va, char *)); + break; + case '%': + putf(putp, ch); + default: + break; + } + } + } +abort:; +} + +void init_printf(void *putp, void (*putf)(void *, char)) +{ + stdout_putf = putf; + stdout_putp = putp; +} + +void tfp_printf(char *fmt, ...) +{ + va_list va; + va_start(va, fmt); + tfp_format(stdout_putp, stdout_putf, fmt, va); + va_end(va); +} + +static void putcp(void *p, char c) +{ + *(*((char **)p))++ = c; +} + +void tfp_sprintf(char *s, char *fmt, ...) +{ + va_list va; + va_start(va, fmt); + tfp_format(&s, putcp, fmt, va); + putcp(&s, 0); + va_end(va); +} + +int puts(const char *s) { + puts_noendl(s); + stdout_putf(stdout_putp, '\n'); + return 1; +} + +int puts_noendl(const char *s) { + while (*s) + stdout_putf(stdout_putp, *s++); + return 1; } \ No newline at end of file