OpenCores
URL https://opencores.org/ocsvn/tcp_socket/tcp_socket/trunk

Subversion Repositories tcp_socket

[/] [tcp_socket/] [trunk/] [source/] [print.h] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 jondawson
////////////////////////////////////////////////////////////////////////////////
2
//
3
//  CHIPS-2.0  print.h
4
//
5
//  :Author: Jonathan P Dawson
6
//  :Date: 22/10/2013
7
//  :email: chips@jondawson.org.uk
8
//  :license: MIT
9
//  :Copyright: Copyright (C) Jonathan P Dawson 2013
10
//
11
//  Print values to stdout. Before including this file, 
12
//  void stdout_put_char(unsigned i) must be defined.
13
//  
14
//  This task is left to the user so that stdout can be directed to uart/socket
15
//  as required.
16
//
17
////////////////////////////////////////////////////////////////////////////////
18
 
19
 
20
//Print a string *string* to stdout
21
void print_string(unsigned string[]){
22
        unsigned i=0;
23
        while(string[i]){
24
                stdout_put_char(string[i]);
25
                i++;
26
        }
27
}
28
 
29
//Print an unsigned int to stdout in hex format
30
void print_uhex(unsigned uhex){
31
        unsigned digit_3 = (uhex >> 12) & 0xf;
32
        unsigned digit_2 = (uhex >> 8) & 0xf;
33
        unsigned digit_1 = (uhex >> 4) & 0xf;
34
        unsigned digit_0 = uhex & 0xf;
35
        if(digit_3 < 9) stdout_put_char(digit_3 | 0x30);
36
        else stdout_put_char(digit_3 + 87);
37
        if(digit_2 < 9) stdout_put_char(digit_2 | 0x30);
38
        else stdout_put_char(digit_2 + 87);
39
        if(digit_1 < 9) stdout_put_char(digit_1 | 0x30);
40
        else stdout_put_char(digit_1 + 87);
41
        if(digit_0 < 9) stdout_put_char(digit_0 | 0x30);
42
        else stdout_put_char(digit_0 + 87);
43
}
44
 
45
//Print an unsigned int to stdout in decimal format
46
//leading 0s will be suppressed
47
void print_udecimal(unsigned udecimal){
48
        unsigned digit;
49
        unsigned significant = 0;
50
        digit = 0;
51
        while(udecimal >= 10000){
52
                udecimal -= 10000;
53
                digit += 1;
54
        }
55
        if(digit | significant){
56
              stdout_put_char(digit | 0x30);
57
              significant = 1;
58
        }
59
        digit = 0;
60
        while(udecimal >= 1000){
61
                udecimal -= 1000;
62
                digit += 1;
63
        }
64
        if(digit | significant){
65
              stdout_put_char(digit | 0x30);
66
              significant = 1;
67
        }
68
        digit = 0;
69
        while(udecimal >= 100){
70
                udecimal -= 100;
71
                digit += 1;
72
        }
73
        if(digit | significant){
74
              stdout_put_char(digit | 0x30);
75
              significant = 1;
76
        }
77
        digit = 0;
78
        while(udecimal >= 10){
79
                udecimal -= 10;
80
                digit += 1;
81
        }
82
        if(digit | significant){
83
              stdout_put_char(digit | 0x30);
84
              significant = 1;
85
        }
86
        stdout_put_char(udecimal | 0x30);
87
}
88
 
89
//Print a signed int to stdout in hex format
90
void print_hex(int hex){
91
        if(hex >= 0){
92
                print_uhex(hex);
93
        } else {
94
                stdout_put_char('-');
95
                print_uhex(-hex);
96
        }
97
}
98
 
99
//Print a signed int to stdout in decimal format
100
//leading 0s will be suppressed
101
void print_decimal(int decimal){
102
        if(decimal >= 0){
103
                print_udecimal(decimal);
104
        } else {
105
                stdout_put_char('-');
106
                print_udecimal(-decimal);
107
        }
108
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.