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

Subversion Repositories zipcpu

[/] [zipcpu/] [trunk/] [sim/] [verilator/] [byteswap.cpp] - Blame information for rev 208

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

Line No. Rev Author Line
1 204 dgisselq
////////////////////////////////////////////////////////////////////////////////
2
//
3
// Filename:    byteswap.cpp
4
//
5
// Project:     Zip CPU -- a small, lightweight, RISC CPU soft core
6
//
7
// Purpose:     To convert between little endian and big endian byte orders,
8
//              and to handle conversions between character strings and
9
//      bit-endian words made from those characters.
10
//
11
//
12
// Creator:     Dan Gisselquist, Ph.D.
13
//              Gisselquist Technology, LLC
14
//
15
////////////////////////////////////////////////////////////////////////////////
16
//
17
// Copyright (C) 2015-2016, Gisselquist Technology, LLC
18
//
19
// This program is free software (firmware): you can redistribute it and/or
20
// modify it under the terms of  the GNU General Public License as published
21
// by the Free Software Foundation, either version 3 of the License, or (at
22
// your option) any later version.
23
//
24
// This program is distributed in the hope that it will be useful, but WITHOUT
25
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
26
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
27
// for more details.
28
//
29
// You should have received a copy of the GNU General Public License along
30
// with this program.  (It's in the $(ROOT)/doc directory.  Run make with no
31
// target there if the PDF file isn't present.)  If not, see
32
// <http://www.gnu.org/licenses/> for a copy.
33
//
34
// License:     GPL, v3, as defined and found on www.gnu.org,
35
//              http://www.gnu.org/licenses/gpl.html
36
//
37
//
38
////////////////////////////////////////////////////////////////////////////////
39
//
40
//
41
#include <stdint.h>
42
#include "byteswap.h"
43
 
44
/*
45
 * byteswap
46
 *
47
 * Given a big (or little) endian word, return a little (or big) endian word.
48
 */
49
uint32_t
50
byteswap(uint32_t v) {
51
        uint32_t        r = 0;
52
 
53
        r = (v & 0x0ff);
54
        r <<= 8; v >>= 8;
55
        r |= (v & 0x0ff);
56
        r <<= 8; v >>= 8;
57
        r |= (v & 0x0ff);
58
        r <<= 8; v >>= 8;
59
        r |= (v & 0x0ff);
60
 
61
        return r;
62
}
63
 
64
 
65
/*
66
 * byteswapbuf
67
 *
68
 * To swap from the byte order of every 32-bit word in the given buffer.
69
 */
70
void
71
byteswapbuf(int ln, uint32_t *buf) {
72
        for(int i=0; i<ln; i++)
73
                buf[i] = byteswap(buf[i]);
74
}
75
 
76
/*
77
 * buildword
78
 *
79
 * Given a pointer within an array of characters, build a 32-bit big-endian
80
 * word from those characters.  Does not require the character pointer to be
81
 * aligned.
82
 */
83
uint32_t
84
buildword(const unsigned char *p) {
85
        uint32_t        r = 0;
86
 
87
        r  = (*p++); r <<= 8;
88
        r |= (*p++); r <<= 8;
89
        r |= (*p++); r <<= 8;
90
        r |= (*p  );
91
 
92
        return r;
93
}
94
 
95
/*
96
 * buildswap
97
 *
98
 * Same as buildword, except that we build a little endian word from the
99
 * characters given to us.  Hence the first character is the low order octet
100
 * of the word.
101
 */
102
uint32_t
103
buildswap(const unsigned char *p) {
104
        uint32_t        r = 0;
105
 
106
        r  = p[3]; r <<= 8;
107
        r |= p[2]; r <<= 8;
108
        r |= p[1]; r <<= 8;
109
        r |= p[0];
110
 
111
        return r;
112
}
113
 
114
 

powered by: WebSVN 2.1.0

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