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

Subversion Repositories light52

[/] [light52/] [trunk/] [tools/] [b51/] [src/] [main.c] - Blame information for rev 4

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 4 ja_rd
/**
2
    @file main.c
3
    @brief Entry point to b51 simulator.
4
 
5
    So farm this program is inly useful for the cosimulation of the light52 test
6
    bench. It does not simulate any peripheral hardware. Besides, this main
7
    program is just a stub: no argument parsing, for starters.
8
 
9
*/
10
 
11
#include <stdint.h>
12
#include <stdbool.h>
13
#include <stdio.h>
14
#include <stdlib.h>
15
#include <string.h>
16
 
17
#include "b51_mcu.h"
18
#include "b51_cpu.h"
19
#include "b51_log.h"
20
 
21
/*-- Local file data ---------------------------------------------------------*/
22
 
23
/** Value of command line parameters */
24
static struct {
25
    char *hex_file_name;
26
    char *trace_logfile_name;
27
    char *console_logfile_name;
28
    uint32_t num_instructions;
29
    bool implement_bcd;
30
} args;
31
 
32
 
33
 
34
/*-- Local function prototypes -----------------------------------------------*/
35
 
36
static bool parse_command_line(int argc, char **argv);
37
static void usage(void);
38
 
39
 
40
/*-- Entry point -------------------------------------------------------------*/
41
 
42
int main(int argc, char **argv){
43
    cpu51_t cpu;
44
    int32_t retval;
45
 
46
    cpu_init(&cpu);
47
 
48
    /* Parse command line... */
49
    if(!parse_command_line(argc, argv)){
50
        exit(2);
51
    }
52
    /* ...and pass options to CPU model */
53
    cpu.options.bcd = args.implement_bcd;
54
 
55
    if(!cpu_load_code(&cpu, args.hex_file_name)){
56
        exit(1);
57
    }
58
 
59
    log_init(args.trace_logfile_name, args.console_logfile_name);
60
    printf("\n\n");
61
 
62
    //cpu_add_breakpoint(&cpu, 0x0003);
63
    cpu_reset(&cpu);
64
    retval = cpu_exec(&cpu, args.num_instructions);
65
 
66
    printf("\n\nExecution finished after %u instructions and %u cycles.\n",
67
           cpu.log.executed_instructions, cpu.cycles);
68
 
69
    switch(retval){
70
    case 1 :
71
        printf("Execution interrupted, cause unknown.\n");
72
        break;
73
    case 2 :
74
        printf("Execution hit a breakpoint.\n");
75
        break;
76
    default :
77
        printf("Execution loop returned invalid code %d\n", retval);
78
        break;
79
    }
80
 
81
    log_close();
82
 
83
    return 0;
84
}
85
 
86
/*-- local functions ---------------------------------------------------------*/
87
 
88
static bool parse_command_line(int argc, char **argv){
89
    uint32_t i;
90
 
91
    /* Fill command line arguments with default values */
92
    args.console_logfile_name = NULL; /* "console_log.txt"; */
93
    args.trace_logfile_name = "sw_log.txt";
94
    args.hex_file_name = NULL;
95
    args.num_instructions = 9e8;
96
    args.implement_bcd = false;
97
 
98
    for(i=1;i<argc;i++){
99
        if(strcmp(argv[i],"--nologs")==0){
100
            /* disable logging */
101
            args.console_logfile_name = NULL;
102
            args.trace_logfile_name = NULL;
103
        }
104
        else if(strncmp(argv[i],"--hex=", strlen("--hex="))==0){
105
            args.hex_file_name = &(argv[i][strlen("--hex=")]);
106
        }
107
        else if(strncmp(argv[i],"--log_con=", strlen("--log_con="))==0){
108
            args.console_logfile_name = &(argv[i][strlen("--log_con=")]);
109
        }
110
        else if(strncmp(argv[i],"--log=", strlen("--log="))==0){
111
            args.trace_logfile_name = &(argv[i][strlen("--log_con=")]);
112
        }
113
        else if(strncmp(argv[i],"--ninst=", strlen("--ninst="))==0){
114
            /* Number of instructions as decimal integer */
115
            if(sscanf(&(argv[i][strlen("--ninst=")]), "%u",
116
                      &(args.num_instructions))==0){
117
                printf("Error: expected decimal integer as argument of --ninst\n\n");
118
                return false;
119
            }
120
        }
121
        else if(strncmp(argv[i],"--bcd", strlen("--bcd"))==0){
122
            printf("Simulating BCD instructions.\n");
123
            args.implement_bcd = true;
124
        }
125
        else{
126
            printf("unknown argument '%s'\n\n",argv[i]);
127
            usage();
128
            return false;
129
        }
130
    }
131
 
132
    if(args.hex_file_name==NULL){
133
        printf("Error: Missing mandatory '--hex=' argument.\n");
134
        usage();
135
        return false;
136
    }
137
 
138
    return true;
139
}
140
 
141
 
142
static void usage(void){
143
    printf("B51: Batch-mode simulator for MCS51 architecture.\n\n");
144
    printf("Usage: b51 [options]\n\n");
145
    printf("Options:\n\n");
146
    printf("  --hex=<filename>       -"
147
           " (mandatory) Name of Intel HEX object code file.\n");
148
    printf("  --nint=<dec. number>   -"
149
           " No. of instructions to run. Defaults to a gazillion.\n");
150
    printf("  --nologs               -"
151
           " Disable console and execution logging.\n");
152
    printf("\n");
153
    printf("The program will load the object file, reset the CPU and execute "
154
           "the specified\nnumber of instructions, then quit.\n");
155
    printf("Simulation will only stop after <nint> instructions, when the CPU "
156
           "enters a\nsingle-instruction endless loop or on an error "
157
           "condition.\n\n");
158
}

powered by: WebSVN 2.1.0

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