10cc8b6ece4b3e757e11a906a81ece292437713abarmvixl// Copyright 2015, ARM Limited 20cc8b6ece4b3e757e11a906a81ece292437713abarmvixl// All rights reserved. 30cc8b6ece4b3e757e11a906a81ece292437713abarmvixl// 40cc8b6ece4b3e757e11a906a81ece292437713abarmvixl// Redistribution and use in source and binary forms, with or without 50cc8b6ece4b3e757e11a906a81ece292437713abarmvixl// modification, are permitted provided that the following conditions are met: 60cc8b6ece4b3e757e11a906a81ece292437713abarmvixl// 70cc8b6ece4b3e757e11a906a81ece292437713abarmvixl// * Redistributions of source code must retain the above copyright notice, 80cc8b6ece4b3e757e11a906a81ece292437713abarmvixl// this list of conditions and the following disclaimer. 90cc8b6ece4b3e757e11a906a81ece292437713abarmvixl// * Redistributions in binary form must reproduce the above copyright notice, 100cc8b6ece4b3e757e11a906a81ece292437713abarmvixl// this list of conditions and the following disclaimer in the documentation 110cc8b6ece4b3e757e11a906a81ece292437713abarmvixl// and/or other materials provided with the distribution. 120cc8b6ece4b3e757e11a906a81ece292437713abarmvixl// * Neither the name of ARM Limited nor the names of its contributors may be 130cc8b6ece4b3e757e11a906a81ece292437713abarmvixl// used to endorse or promote products derived from this software without 140cc8b6ece4b3e757e11a906a81ece292437713abarmvixl// specific prior written permission. 150cc8b6ece4b3e757e11a906a81ece292437713abarmvixl// 160cc8b6ece4b3e757e11a906a81ece292437713abarmvixl// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS CONTRIBUTORS "AS IS" AND 170cc8b6ece4b3e757e11a906a81ece292437713abarmvixl// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 180cc8b6ece4b3e757e11a906a81ece292437713abarmvixl// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 190cc8b6ece4b3e757e11a906a81ece292437713abarmvixl// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 200cc8b6ece4b3e757e11a906a81ece292437713abarmvixl// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 210cc8b6ece4b3e757e11a906a81ece292437713abarmvixl// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 220cc8b6ece4b3e757e11a906a81ece292437713abarmvixl// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 230cc8b6ece4b3e757e11a906a81ece292437713abarmvixl// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 240cc8b6ece4b3e757e11a906a81ece292437713abarmvixl// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 250cc8b6ece4b3e757e11a906a81ece292437713abarmvixl// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 260cc8b6ece4b3e757e11a906a81ece292437713abarmvixl 270cc8b6ece4b3e757e11a906a81ece292437713abarmvixl#include "examples.h" 280cc8b6ece4b3e757e11a906a81ece292437713abarmvixl 290cc8b6ece4b3e757e11a906a81ece292437713abarmvixl#define BUF_SIZE (4096) 300cc8b6ece4b3e757e11a906a81ece292437713abarmvixl#define __ masm-> 310cc8b6ece4b3e757e11a906a81ece292437713abarmvixl 320cc8b6ece4b3e757e11a906a81ece292437713abarmvixlvoid GenerateCrc32(MacroAssembler* masm) { 330cc8b6ece4b3e757e11a906a81ece292437713abarmvixl // uint32_t crc32(const char *msg, size_t length) 340cc8b6ece4b3e757e11a906a81ece292437713abarmvixl // Argument location: 350cc8b6ece4b3e757e11a906a81ece292437713abarmvixl // msg (pointer) -> x0 360cc8b6ece4b3e757e11a906a81ece292437713abarmvixl // length (integer) -> x1 370cc8b6ece4b3e757e11a906a81ece292437713abarmvixl 380cc8b6ece4b3e757e11a906a81ece292437713abarmvixl // This example computes CRC32CB on an input array of a given size 390cc8b6ece4b3e757e11a906a81ece292437713abarmvixl // and returns the resulting checksum in w0. 400cc8b6ece4b3e757e11a906a81ece292437713abarmvixl 410cc8b6ece4b3e757e11a906a81ece292437713abarmvixl Label loop, end; 420cc8b6ece4b3e757e11a906a81ece292437713abarmvixl 430cc8b6ece4b3e757e11a906a81ece292437713abarmvixl // Move input array to temp register so we can re-use w0 as return register. 440cc8b6ece4b3e757e11a906a81ece292437713abarmvixl __ Mov(x2, x0); 450cc8b6ece4b3e757e11a906a81ece292437713abarmvixl 460cc8b6ece4b3e757e11a906a81ece292437713abarmvixl // Initial remainder for the checksum. If length=0, then this value will 470cc8b6ece4b3e757e11a906a81ece292437713abarmvixl // be returned. 480cc8b6ece4b3e757e11a906a81ece292437713abarmvixl __ Mov(w0, 0xffffffff); 490cc8b6ece4b3e757e11a906a81ece292437713abarmvixl 500cc8b6ece4b3e757e11a906a81ece292437713abarmvixl // Loop for iterating through the array, starting at msg[0]. 510cc8b6ece4b3e757e11a906a81ece292437713abarmvixl __ Bind(&loop); 520cc8b6ece4b3e757e11a906a81ece292437713abarmvixl 530cc8b6ece4b3e757e11a906a81ece292437713abarmvixl // If no more elements to process, then exit function. 540cc8b6ece4b3e757e11a906a81ece292437713abarmvixl __ Cbz(x1, &end); 550cc8b6ece4b3e757e11a906a81ece292437713abarmvixl __ Sub(x1, x1, 1); 560cc8b6ece4b3e757e11a906a81ece292437713abarmvixl 570cc8b6ece4b3e757e11a906a81ece292437713abarmvixl // Compute checksum for msg[i]. 580cc8b6ece4b3e757e11a906a81ece292437713abarmvixl __ Ldrb(w3, MemOperand(x2, 1, PostIndex)); 590cc8b6ece4b3e757e11a906a81ece292437713abarmvixl __ Crc32b(w0, w0, w3); 600cc8b6ece4b3e757e11a906a81ece292437713abarmvixl __ B(&loop); 610cc8b6ece4b3e757e11a906a81ece292437713abarmvixl 620cc8b6ece4b3e757e11a906a81ece292437713abarmvixl __ Bind(&end); 630cc8b6ece4b3e757e11a906a81ece292437713abarmvixl 640cc8b6ece4b3e757e11a906a81ece292437713abarmvixl __ Ret(); 650cc8b6ece4b3e757e11a906a81ece292437713abarmvixl} 660cc8b6ece4b3e757e11a906a81ece292437713abarmvixl 670cc8b6ece4b3e757e11a906a81ece292437713abarmvixl#ifndef TEST_EXAMPLES 680cc8b6ece4b3e757e11a906a81ece292437713abarmvixl 690cc8b6ece4b3e757e11a906a81ece292437713abarmvixlvoid runExample(const char *msg) { 700cc8b6ece4b3e757e11a906a81ece292437713abarmvixl // Create and initialize the assembler and the simulator. 710cc8b6ece4b3e757e11a906a81ece292437713abarmvixl byte assm_buf[BUF_SIZE]; 720cc8b6ece4b3e757e11a906a81ece292437713abarmvixl MacroAssembler masm(assm_buf, BUF_SIZE); 730cc8b6ece4b3e757e11a906a81ece292437713abarmvixl 740cc8b6ece4b3e757e11a906a81ece292437713abarmvixl // Generate the code for the example function. 750cc8b6ece4b3e757e11a906a81ece292437713abarmvixl Label func; 760cc8b6ece4b3e757e11a906a81ece292437713abarmvixl masm.Bind(&func); 770cc8b6ece4b3e757e11a906a81ece292437713abarmvixl GenerateCrc32(&masm); 780cc8b6ece4b3e757e11a906a81ece292437713abarmvixl masm.FinalizeCode(); 790cc8b6ece4b3e757e11a906a81ece292437713abarmvixl 800cc8b6ece4b3e757e11a906a81ece292437713abarmvixl#ifdef USE_SIMULATOR 810cc8b6ece4b3e757e11a906a81ece292437713abarmvixl // Run example function in the simulator. 820cc8b6ece4b3e757e11a906a81ece292437713abarmvixl uintptr_t msg_addr = reinterpret_cast<uintptr_t>(msg); 830cc8b6ece4b3e757e11a906a81ece292437713abarmvixl size_t msg_size = strlen(msg); 840cc8b6ece4b3e757e11a906a81ece292437713abarmvixl Decoder decoder; 850cc8b6ece4b3e757e11a906a81ece292437713abarmvixl Simulator simulator(&decoder); 860cc8b6ece4b3e757e11a906a81ece292437713abarmvixl simulator.set_xreg(0, msg_addr); 870cc8b6ece4b3e757e11a906a81ece292437713abarmvixl simulator.set_xreg(1, msg_size); 880cc8b6ece4b3e757e11a906a81ece292437713abarmvixl simulator.RunFrom(masm.GetLabelAddress<Instruction*>(&func)); 890cc8b6ece4b3e757e11a906a81ece292437713abarmvixl printf("crc32(\"%s\")=0x%x\n", msg, simulator.wreg(0)); 900cc8b6ece4b3e757e11a906a81ece292437713abarmvixl#else 910cc8b6ece4b3e757e11a906a81ece292437713abarmvixl // Run example function natively. 920cc8b6ece4b3e757e11a906a81ece292437713abarmvixl printf("Not yet implemented.\n"); 930cc8b6ece4b3e757e11a906a81ece292437713abarmvixl USE(msg); 940cc8b6ece4b3e757e11a906a81ece292437713abarmvixl#endif // USE_SIMULATOR 950cc8b6ece4b3e757e11a906a81ece292437713abarmvixl} 960cc8b6ece4b3e757e11a906a81ece292437713abarmvixl 970cc8b6ece4b3e757e11a906a81ece292437713abarmvixl 980cc8b6ece4b3e757e11a906a81ece292437713abarmvixlint main(void) { 990cc8b6ece4b3e757e11a906a81ece292437713abarmvixl runExample("Hello World!"); 1000cc8b6ece4b3e757e11a906a81ece292437713abarmvixl runExample("do"); 1010cc8b6ece4b3e757e11a906a81ece292437713abarmvixl runExample("1"); 1020cc8b6ece4b3e757e11a906a81ece292437713abarmvixl runExample(""); 1030cc8b6ece4b3e757e11a906a81ece292437713abarmvixl 1040cc8b6ece4b3e757e11a906a81ece292437713abarmvixl return 0; 1050cc8b6ece4b3e757e11a906a81ece292437713abarmvixl} 1060cc8b6ece4b3e757e11a906a81ece292437713abarmvixl#endif // TEST_EXAMPLES 107