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 123

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 123 markom
        int breakpoint = 0;
44
 
45 7 jrydberg
#if STACK_ARGS
46 123 markom
        fmtaddr = eval_mem32(stackaddr,&breakpoint);
47 7 jrydberg
#else
48
        fmtaddr = regparam;
49
#endif
50
 
51
        i = 0;
52 123 markom
        while (eval_mem8(fmtaddr,&breakpoint) != '\0') {
53
                fmtstr[i++] = eval_mem8(fmtaddr,&breakpoint);
54 7 jrydberg
                fmtaddr++;
55
                if (i == FMTLEN - 1)
56
                        break;
57
        }
58
        fmtstr[i] = '\0';
59
 
60 75 lampret
        return fmtstr;
61
}
62
 
63
void simprintf(unsigned long stackaddr, unsigned long regparam)
64
{
65
        unsigned long fmtaddr;
66
        FILE *f;
67
        int i = 0;
68 123 markom
        int breakpoint = 0;
69
 
70 75 lampret
        simgetstr(stackaddr, regparam);
71
 
72 7 jrydberg
        debug("simprintf: stackaddr: 0x%.8lx", stackaddr);
73
        if ((f = fopen("stdout.txt", "a+"))) {
74
                unsigned long arg;
75
                unsigned long argaddr;
76
                unsigned char regstr[5];
77
                char *fmtstrend;
78
                char *fmtstrpart = fmtstr;
79
 
80
#if STACK_ARGS
81
                argaddr = stackaddr;
82
#else
83
                argaddr = 3;
84
#endif
85
                debug("simprintf: %s", fmtstrpart);
86
                while(strlen(fmtstrpart)) {
87 44 lampret
                        debug("simprintf(): 1");
88 7 jrydberg
                        if ((fmtstrend = strstr(fmtstrpart + 1, "%")))
89
                                *fmtstrend = '\0';
90 44 lampret
                        debug(" 2");
91 7 jrydberg
                        if (strstr(fmtstrpart, "%")) {
92 44 lampret
                                debug(" 3");
93 7 jrydberg
#if STACK_ARGS
94
                                argaddr += 4;
95 123 markom
                                arg = eval_mem32(argaddr,&breakpoint);
96 7 jrydberg
#else
97
                                sprintf(regstr, "r%u", ++argaddr);
98
                                arg = eval_reg(regstr);
99
#endif
100 44 lampret
                                debug(" 4: fmtstrpart=%p fmtstrpart=%s arg=%p", fmtstrpart, fmtstrpart, arg);
101
                                if (strncmp(fmtstrpart, "%s", 2) == 0) {
102
                                        int len = 0;
103
                                        char *str;
104 123 markom
                                        for(; eval_mem8(arg++,&breakpoint); len++);
105 44 lampret
                                        len++;  /* for null char */
106
                                        arg -= len;
107
                                        str = (char *)malloc(len);
108
                                        len = 0;
109 123 markom
                                        for(; eval_mem8(arg,&breakpoint); len++)
110
                                                *(str+len) = eval_mem8(arg++,&breakpoint);
111
                                        *(str+len) = eval_mem8(arg,&breakpoint); /* null ch */
112 44 lampret
                                        /* debug("4a: len=%d str=%s\n", len, str);
113
                                        debug("4b:"); */
114
                                        fprintf(f, fmtstrpart, str);
115
                                        free(str);
116
                                } else
117
                                        fprintf(f, fmtstrpart, arg);
118 7 jrydberg
                        } else {
119 44 lampret
                                debug(" 5");
120 7 jrydberg
                                fprintf(f, fmtstrpart);
121
                                debug(fmtstrpart);
122
                        }
123
                        if (!fmtstrend)
124
                                break;
125 44 lampret
                        debug(" 6");
126 7 jrydberg
                        fmtstrpart = fmtstrend;
127
                        *fmtstrpart = '%';
128 44 lampret
                        debug(" 7");
129 7 jrydberg
                }
130
 
131 44 lampret
                debug(" 8");
132 7 jrydberg
                if (fclose(f))
133
                        perror(strerror(errno));
134
        }
135
        else
136
                perror(strerror(errno));
137
 
138
}

powered by: WebSVN 2.1.0

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