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 44

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

powered by: WebSVN 2.1.0

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