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

Subversion Repositories or1k

[/] [or1k/] [tags/] [nog_patch_52/] [or1ksim/] [support/] [simprintf.c] - Blame information for rev 75

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 7 jrydberg
/* libc.c -- dummy C library simulation
2
   Copyright (C) 1999 Damjan Lampret, lampret@opencores.org
3
 
4
This file is part of OpenRISC 1000 Architectural Simulator.
5
 
6
This program is free software; you can redistribute it and/or modify
7
it under the terms of the GNU General Public License as published by
8
the Free Software Foundation; either version 2 of the License, or
9
(at your option) any later version.
10
 
11
This program is distributed in the hope that it will be useful,
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
GNU General Public License for more details.
15
 
16
You should have received a copy of the GNU General Public License
17
along with this program; if not, write to the Free Software
18
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19
 
20
/* Debugger LIBC functions. Working, but VERY, VERY ugly written.
21
I wrote following code when basic simulator started to work and I was
22
desperate to use some printfs in my debugged code. And it was
23
also used to get some output from Dhrystone MIPS benchmark. */
24
 
25
#include <stdio.h>
26
#include <string.h>
27
#include <errno.h>
28
#include <stdarg.h>
29
 
30
#include <abstract.h>
31
#include <arch.h>
32
 
33
/* Length of printf format string */
34
#define FMTLEN 2000
35
 
36 75 lampret
char fmtstr[FMTLEN];
37
 
38
char *simgetstr(unsigned long stackaddr, unsigned long regparam)
39 7 jrydberg
{
40
        unsigned long fmtaddr;
41
        FILE *f;
42
        int i;
43
 
44
#if STACK_ARGS
45
        fmtaddr = eval_mem32(stackaddr);
46
#else
47
        fmtaddr = regparam;
48
#endif
49
 
50
        i = 0;
51
        while (eval_mem8(fmtaddr) != '\0') {
52
                fmtstr[i++] = eval_mem8(fmtaddr);
53
                fmtaddr++;
54
                if (i == FMTLEN - 1)
55
                        break;
56
        }
57
        fmtstr[i] = '\0';
58
 
59 75 lampret
        return fmtstr;
60
}
61
 
62
void simprintf(unsigned long stackaddr, unsigned long regparam)
63
{
64
        unsigned long fmtaddr;
65
        FILE *f;
66
        int i = 0;
67
 
68
        simgetstr(stackaddr, regparam);
69
 
70 7 jrydberg
        debug("simprintf: stackaddr: 0x%.8lx", stackaddr);
71
        if ((f = fopen("stdout.txt", "a+"))) {
72
                unsigned long arg;
73
                unsigned long argaddr;
74
                unsigned char regstr[5];
75
                char *fmtstrend;
76
                char *fmtstrpart = fmtstr;
77
 
78
#if STACK_ARGS
79
                argaddr = stackaddr;
80
#else
81
                argaddr = 3;
82
#endif
83
                debug("simprintf: %s", fmtstrpart);
84
                while(strlen(fmtstrpart)) {
85 44 lampret
                        debug("simprintf(): 1");
86 7 jrydberg
                        if ((fmtstrend = strstr(fmtstrpart + 1, "%")))
87
                                *fmtstrend = '\0';
88 44 lampret
                        debug(" 2");
89 7 jrydberg
                        if (strstr(fmtstrpart, "%")) {
90 44 lampret
                                debug(" 3");
91 7 jrydberg
#if STACK_ARGS
92
                                argaddr += 4;
93
                                arg = eval_mem32(argaddr);
94
#else
95
                                sprintf(regstr, "r%u", ++argaddr);
96
                                arg = eval_reg(regstr);
97
#endif
98 44 lampret
                                debug(" 4: fmtstrpart=%p fmtstrpart=%s arg=%p", fmtstrpart, fmtstrpart, arg);
99
                                if (strncmp(fmtstrpart, "%s", 2) == 0) {
100
                                        int len = 0;
101
                                        char *str;
102
                                        for(; eval_mem8(arg++); len++);
103
                                        len++;  /* for null char */
104
                                        arg -= len;
105
                                        str = (char *)malloc(len);
106
                                        len = 0;
107
                                        for(; eval_mem8(arg); len++)
108
                                                *(str+len) = eval_mem8(arg++);
109
                                        *(str+len) = eval_mem8(arg); /* null ch */
110
                                        /* debug("4a: len=%d str=%s\n", len, str);
111
                                        debug("4b:"); */
112
                                        fprintf(f, fmtstrpart, str);
113
                                        free(str);
114
                                } else
115
                                        fprintf(f, fmtstrpart, arg);
116 7 jrydberg
                        } else {
117 44 lampret
                                debug(" 5");
118 7 jrydberg
                                fprintf(f, fmtstrpart);
119
                                debug(fmtstrpart);
120
                        }
121
                        if (!fmtstrend)
122
                                break;
123 44 lampret
                        debug(" 6");
124 7 jrydberg
                        fmtstrpart = fmtstrend;
125
                        *fmtstrpart = '%';
126 44 lampret
                        debug(" 7");
127 7 jrydberg
                }
128
 
129 44 lampret
                debug(" 8");
130 7 jrydberg
                if (fclose(f))
131
                        perror(strerror(errno));
132
        }
133
        else
134
                perror(strerror(errno));
135
 
136
}

powered by: WebSVN 2.1.0

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