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

Subversion Repositories w11

[/] [w11/] [tags/] [w11a_V0.7/] [tools/] [src/] [librtools/] [RosPrintBvi.cpp] - Blame information for rev 10

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

Line No. Rev Author Line
1 10 wfjm
// $Id: RosPrintBvi.cpp 368 2011-03-12 09:58:53Z mueller $
2
//
3
// Copyright 2011- by Walter F.J. Mueller <W.F.J.Mueller@gsi.de>
4
//
5
// This program is free software; you may redistribute and/or modify it under
6
// the terms of the GNU General Public License as published by the Free
7
// Software Foundation, either version 2, or at your option any later version.
8
//
9
// This program is distributed in the hope that it will be useful, but
10
// WITHOUT ANY WARRANTY, without even the implied warranty of MERCHANTABILITY
11
// or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12
// for complete details.
13
// 
14
// Revision History: 
15
// Date         Rev Version  Comment
16
// 2011-03-12   368   1.0.1  allow base=0, will print in hex,oct and bin
17
// 2011-03-05   366   1.0    Initial version
18
// ---------------------------------------------------------------------------
19
 
20
/*!
21
  \file
22
  \version $Id: RosPrintBvi.cpp 368 2011-03-12 09:58:53Z mueller $
23
  \brief   Implemenation of RosPrintBvi .
24
*/
25
 
26
#include <stdexcept>
27
 
28
#include "RosPrintBvi.hpp"
29
 
30
using namespace std;
31
using namespace Retro;
32
 
33
/*!
34
  \class Retro::RosPrintBvi
35
  \brief FIXME_docs.
36
*/
37
 
38
//------------------------------------------+-----------------------------------
39
//! Constructor. FIXME_docs
40
 
41
RosPrintBvi::RosPrintBvi(uint8_t val, size_t base, size_t nbit)
42
  : fVal((uint8_t)val),
43
    fBase(base),
44
    fNbit(nbit)
45
{
46
  if (base!=0 && base!=2 && base!=8 && base!=16)
47
    throw invalid_argument("RosPrintBvi::ctor: base must be 0,2,8, or 16");
48
  if (nbit<1 || nbit>8)
49
    throw invalid_argument("RosPrintBvi::ctor: nbit must be in 1,..,8");
50
}
51
 
52
//------------------------------------------+-----------------------------------
53
//! Constructor. FIXME_docs
54
 
55
RosPrintBvi::RosPrintBvi(uint16_t val, size_t base, size_t nbit)
56
  : fVal((uint16_t)val),
57
    fBase(base),
58
    fNbit(nbit)
59
{
60
  if (base!=0 && base!=2 && base!=8 && base!=16)
61
    throw invalid_argument("RosPrintBvi::ctor: base must be 0,2,8, or 16");
62
  if (nbit<1 || nbit>16)
63
    throw invalid_argument("RosPrintBvi::ctor: nbit must be in 1,..,16");
64
}
65
 
66
//------------------------------------------+-----------------------------------
67
//! Constructor. FIXME_docs
68
 
69
RosPrintBvi::RosPrintBvi(uint32_t val, size_t base, size_t nbit)
70
  : fVal(val),
71
    fBase(base),
72
    fNbit(nbit)
73
{
74
  if (base!=0 && base!=2 && base!=8 && base!=16)
75
    throw invalid_argument("RosPrintBvi::ctor: base must be 0,2,8, or 16");
76
  if (nbit<1 || nbit>32)
77
    throw invalid_argument("RosPrintBvi::ctor: nbit must be in 1,..,32");
78
}
79
 
80
//------------------------------------------+-----------------------------------
81
//! FIXME_docs
82
 
83
void RosPrintBvi::Print(std::ostream& os) const
84
{
85
  if (fBase == 0) {
86
    os << RosPrintBvi(fVal, 16, fNbit) << "  "
87
       << RosPrintBvi(fVal,  8, fNbit) << "  "
88
       << RosPrintBvi(fVal,  2, fNbit);
89
    return;
90
  }
91
 
92
  char buf[33];
93
  Convert(buf);
94
  os << buf;
95
  return;
96
}
97
 
98
//------------------------------------------+-----------------------------------
99
//! FIXME_docs
100
 
101
void RosPrintBvi::Print(std::string& os) const
102
{
103
  if (fBase == 0) {
104
    os << RosPrintBvi(fVal, 16, fNbit);
105
    os += "  ";
106
    os << RosPrintBvi(fVal,  8, fNbit);
107
    os += "  ";
108
    os << RosPrintBvi(fVal,  2, fNbit);
109
    return;
110
  }
111
 
112
  char buf[33];
113
  Convert(buf);
114
  os += buf;
115
  return;
116
}
117
 
118
//------------------------------------------+-----------------------------------
119
//! FIXME_docs
120
 
121
void RosPrintBvi::Convert(char* pbuf) const
122
{
123
 
124
  size_t nwidth = 1;
125
  if (fBase ==  8) nwidth = 3;
126
  if (fBase == 16) nwidth = 4;
127
  uint32_t nmask = (1<<nwidth)-1;
128
 
129
  size_t ndig = (fNbit+nwidth-1)/nwidth;
130
 
131
  for (size_t i=ndig; i>0; i--) {
132
    uint32_t nibble = ((fVal)>>((i-1)*nwidth)) & nmask;
133
    nibble += (nibble <= 9) ? '0' : ('a'-10);
134
    *pbuf++ = (char) nibble;
135
  }
136
 
137
  *pbuf++ = '\0';
138
 
139
  return;
140
}
141
 
142
//------------------------------------------+-----------------------------------
143
#if (defined(Retro_NoInline) || defined(Retro_RosPrintBvi_NoInline))
144
#define inline
145
#include "RosPrintBvi.ipp"
146
#undef  inline
147
#endif

powered by: WebSVN 2.1.0

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