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

Subversion Repositories lxp32

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

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 9 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::write(const char *data,std::size_t n) {
26
        writeData(data,n);
27
        _size+=n;
28
}
29
 
30
void OutputWriter::pad(std::size_t size) {
31
        static char zeros[256]; // static objects are zero-initialized
32
        while(size>0) {
33
                auto n=std::min<std::size_t>(size,256);
34
                write(zeros,n);
35
                size-=n;
36
        }
37
}
38
 
39
std::size_t OutputWriter::size() const {
40
        return _size;
41
}
42
 
43
/*
44
 * BinaryOutputWriter members
45
 */
46
 
47
BinaryOutputWriter::BinaryOutputWriter(const std::string &filename):
48
        _filename(filename),
49
        _os(filename,std::ios_base::out|std::ios_base::binary)
50
{
51
        if(!_os) throw std::runtime_error("Cannot open \""+filename+"\" for writing");
52
}
53
 
54
void BinaryOutputWriter::writeData(const char *data,std::size_t n) {
55
        _os.write(data,n);
56
}
57
 
58
void BinaryOutputWriter::abort() {
59
        _os.close();
60
        std::remove(_filename.c_str());
61
}
62
 
63
/*
64
 * TextOutputWriter members
65
 */
66
 
67
TextOutputWriter::TextOutputWriter(const std::string &filename,Format f):
68
        _filename(filename),
69
        _os(filename,std::ios_base::out),
70
        _fmt(f)
71
{
72
        if(!_os) throw std::runtime_error("Cannot open \""+filename+"\" for writing");
73
}
74
 
75
TextOutputWriter::~TextOutputWriter() {
76
        if(!_buf.empty()) {
77
                assert(_buf.size()<4);
78
                pad(4-_buf.size());
79
        }
80
}
81
 
82
void TextOutputWriter::writeData(const char *data,std::size_t n) {
83
        while(n>0) {
84
                assert(_buf.size()<4);
85
                auto count=std::min(4-_buf.size(),n);
86
                _buf.append(data,count);
87
                data+=count;
88
                n-=count;
89
 
90
                if(_buf.size()<4) continue;
91
 
92
                assert(_buf.size()==4);
93
 
94
                std::uint32_t word=(static_cast<unsigned char>(_buf[3])<<24)|
95
                        (static_cast<unsigned char>(_buf[2])<<16)|
96
                        (static_cast<unsigned char>(_buf[1])<<8)|
97
                        static_cast<unsigned char>(_buf[0]);
98
 
99
                if(_fmt==Bin) _os<<Utils::bin(word)<<std::endl;
100
                else if(_fmt==Dec) _os<<word<<std::endl;
101
                else _os<<Utils::hex(word)<<std::endl;
102
                _buf.clear();
103
        }
104
}
105
 
106
void TextOutputWriter::abort() {
107
        _os.close();
108
        std::remove(_filename.c_str());
109
}

powered by: WebSVN 2.1.0

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