summaryrefslogtreecommitdiffstatshomepage
path: root/src/tools/nltool.c
blob: a9ba2b56b073ea6c57a08d9c7e9236ec291e979e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/***************************************************************************

    nltool.c

    Simple tool to debug netlists outside MAME.

****************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "astring.h"
#include "corefile.h"
#include "corestr.h"
#include "sha1.h"
#include "netlist/nl_base.h"
#include "netlist/nl_setup.h"
#include "options.h"

/***************************************************************************
    MAME COMPATIBILITY ...
***************************************************************************/

#if 0
void mame_printf_warning(const char *format, ...)
{
    va_list argptr;

    /* do the output */
    va_start(argptr, format);
    vprintf(format, argptr);
    va_end(argptr);
}
#endif

void *malloc_file_line(size_t size, const char *file, int line)
{
    // allocate the memory and fail if we can't
    void *ret = osd_malloc(size);
    memset(ret, 0, size);
    return ret;
}

void *malloc_array_file_line(size_t size, const char *file, int line)
{
    // allocate the memory and fail if we can't
    void *ret = osd_malloc_array(size);
    memset(ret, 0, size);
    return ret;
}

void free_file_line( void *memory, const char *file, int line )
{
    osd_free( memory );
}


struct options_entry oplist[] =
{
    { "time_to_run;ttr", "1.0", OPTION_FLOAT,   "time to run the emulation (seconds)" },
    { "f",               "-",   OPTION_STRING,  "file to process (default is stdin)" },
    { "help;h",          "0",   OPTION_BOOLEAN, "display help" },
    { NULL }
};

/***************************************************************************
    CORE IMPLEMENTATION
***************************************************************************/

const char *filetobuf(pstring fname)
{
    static pstring pbuf = "";

    if (fname == "-")
    {
        char lbuf[1024];
        while (!feof(stdin))
        {
            fgets(lbuf, 1024, stdin);
            pbuf += lbuf;
        }
        printf("%d\n",*(pbuf.right(1).cstr()+1));
        return pbuf.cstr();
    }
    else
    {
        FILE *f;
        f = fopen(fname, "rb");
        fseek(f, 0, SEEK_END);
        long fsize = ftell(f);
        fseek(f, 0, SEEK_SET);

        char *buf = (char *) malloc(fsize);
        fread(buf, fsize, 1, f);
        buf[fsize] = 0;
        fclose(f);
        return buf;
    }
}

class netlist_tool_t : public netlist_base_t
{
public:

    netlist_tool_t()
    : netlist_base_t(), m_setup(NULL)
    {

    }

    virtual ~netlist_tool_t() { };

    void read_netlist(const char *buffer)
    {
        m_setup = new netlist_setup_t(*this);
        this->set_clock_freq(NETLIST_CLOCK);

        // register additional devices
        //m_setup->factory().register_device<nld_analog_callback>( "NETDEV_CALLBACK", "nld_analog_callback");

        // read the netlist ...
        //m_setup_func(*m_setup);

        m_setup->parse(buffer);

        // start devices
        m_setup->start_devices();
        m_setup->resolve_inputs();
        // reset
        this->reset();
    }

protected:

    void vfatalerror(const char *format, va_list ap) const
    {
        vprintf(format, ap);
        throw;
    }

private:
    netlist_setup_t *m_setup;
};

void usage(core_options &opts)
{
    astring buffer;
    fprintf(stderr,
        "Usage:\n"
        "  nltool -help\n"
        "  nltool [options]\n"
        "\n"
        "Where:\n"
    );
    fprintf(stderr, "%s\n", opts.output_help(buffer));
}

/*-------------------------------------------------
    main - primary entry point
-------------------------------------------------*/

int main(int argc, char *argv[])
{
	//int result;
	netlist_tool_t nt;
	core_options opts(oplist);
	astring aerror("");
    osd_ticks_t t = osd_ticks();

    fprintf(stderr, "%s", "WARNING: This is Work In Progress! - It may fail anytime\n");
	if (!opts.parse_command_line(argc, argv, OPTION_PRIORITY_DEFAULT, aerror))
	{
        fprintf(stderr, "%s\n", aerror.cstr());
        usage(opts);
        return 1;
	}
	if (opts.bool_value("h"))
	{
        usage(opts);
        return 1;
	}

    nt.read_netlist(filetobuf(opts.value("f")));
    double ttr = opts.float_value("ttr");

    INT64 tt = ttr * NETLIST_CLOCK;

    printf("startup time ==> %5.3f\n", (double) (osd_ticks() - t) / (double) osd_ticks_per_second() );
    printf("runnning ...\n");
    t = osd_ticks();
    while (tt>0)
    {
        INT32 tr = MIN(tt, NETLIST_CLOCK / 10);
        tt -= tr;
        nt.process_queue(tr);
    }
    double emutime = (double) (osd_ticks() - t) / (double) osd_ticks_per_second();
    printf("%f seconds emulation took %f real time ==> %5.2f%%\n", ttr, emutime, ttr/emutime*100.0);
    
    return 0;
}