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 173

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

powered by: WebSVN 2.1.0

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