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

Subversion Repositories lxp32

[/] [lxp32/] [trunk/] [tools/] [src/] [lxp32asm/] [outputwriter.cpp] - Blame information for rev 2

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 ring0_mipt
/*
2
 * Copyright (c) 2016 by Alex I. Kuznetsov.
3
 *
4
 * Part of the LXP32 CPU IP core.
5
 *
6
 * This module implements members of the OutputWriter class
7
 * and its derived classes.
8
 */
9
 
10
#include "outputwriter.h"
11
#include "utils.h"
12
 
13
#include <iostream>
14
#include <iomanip>
15
#include <stdexcept>
16
#include <algorithm>
17
#include <cassert>
18
#include <cstdint>
19
#include <cstdio>
20
 
21
/*
22
 * OutputWriter members
23
 */
24
 
25
void OutputWriter::pad(std::size_t size) {
26
        static char zeros[256]; // static objects are zero-initialized
27
        while(size>0) {
28
                auto n=std::min<std::size_t>(size,256);
29
                write(zeros,n);
30
                size-=n;
31
        }
32
}
33
 
34
/*
35
 * BinaryOutputWriter members
36
 */
37
 
38
BinaryOutputWriter::BinaryOutputWriter(const std::string &filename):
39
        _filename(filename),
40
        _os(filename,std::ios_base::out|std::ios_base::binary)
41
{
42
        if(!_os) throw std::runtime_error("Cannot open \""+filename+"\" for writing");
43
}
44
 
45
void BinaryOutputWriter::write(const char *data,std::size_t n) {
46
        _os.write(data,n);
47
}
48
 
49
void BinaryOutputWriter::abort() {
50
        _os.close();
51
        std::remove(_filename.c_str());
52
}
53
 
54
/*
55
 * TextOutputWriter members
56
 */
57
 
58
TextOutputWriter::TextOutputWriter(const std::string &filename,Format f):
59
        _filename(filename),
60
        _os(filename,std::ios_base::out),
61
        _fmt(f)
62
{
63
        if(!_os) throw std::runtime_error("Cannot open \""+filename+"\" for writing");
64
}
65
 
66
TextOutputWriter::~TextOutputWriter() {
67
        if(!_buf.empty()) {
68
                assert(_buf.size()<4);
69
                pad(4-_buf.size());
70
        }
71
}
72
 
73
void TextOutputWriter::write(const char *data,std::size_t n) {
74
        while(n>0) {
75
                assert(_buf.size()<4);
76
                auto count=std::min(4-_buf.size(),n);
77
                _buf.append(data,count);
78
                data+=count;
79
                n-=count;
80
 
81
                if(_buf.size()<4) continue;
82
 
83
                assert(_buf.size()==4);
84
 
85
                std::uint32_t word=(static_cast<unsigned char>(_buf[3])<<24)|
86
                        (static_cast<unsigned char>(_buf[2])<<16)|
87
                        (static_cast<unsigned char>(_buf[1])<<8)|
88
                        static_cast<unsigned char>(_buf[0]);
89
 
90
                if(_fmt==Bin) _os<<Utils::bin(word)<<std::endl;
91
                else if(_fmt==Dec) _os<<word<<std::endl;
92
                else _os<<Utils::hex(word)<<std::endl;
93
                _buf.clear();
94
        }
95
}
96
 
97
void TextOutputWriter::abort() {
98
        _os.close();
99
        std::remove(_filename.c_str());
100
}

powered by: WebSVN 2.1.0

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